Data Structures Flashcards

1
Q

Explain the difference between Stacks and Queues

A

Stacks VS Queues:

Stacks:

  • Can be built with Arrays and Linked Lists
  • Arrays allow cache locality, which makes it technically faster since their items are right next to each other in memory
  • Linked lists items are scattered all over memory and have extra memory onto them

Queues:

  • Never build with an array ONLY LINKED LISTS
  • Because we would have to shift the indexes everytime
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are Queues and what they can be used for?

A

Queues (FIFO):

  • lookup O(n)
  • enqueue O(n) – aka push aka add to the queue
  • dequeue O(1) – aka pop aka remove the first person in line
  • peek O(1) – whats the first item that’ll come up?
  • First one in, first out (ROLLERCOASTER LINE)
  • Used for:

—- if you had any waitlist to buy tix for a concert, reservations, uber, lyft

—- a printer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Should you use an array to build a Queue?

A

Never use an ARRAY to build a QUEUE

  • its inefficient
  • removing the first item, so if you use an array you will have to shift the indexes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a Stack and what can it be used for?

A

STACK (LIFO):

  • lookup O(n)
  • pop O(1)
  • push O(1)
  • peek O(1) – view the top plate
  • Last one in, first one out
  • Useful for:
    • — Need to know the very last value that was added
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the pros and cons for Linked Lists?

A

LINKED LISTS

GOOD:

  • Fast Insertion
  • Fast Deletion
  • Ordered
  • Flexible Size

BAD:

  • Slow lookup
  • More memory
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Difference between a Singly vs Doubly Linked list?

A

Singly vs Doubly (When to use)

  • Singly

— simple implementation

— less memory

– Cannot traverse from back to front

Good option when:

  • Less memory or memory is expensive
  • Want to do fast insertion and deletion

Doubly

  • Can traverse the list
  • If you need to delete a previous node, you don’t need to traverse from the head node and find what the previous node is
  • Requires more memory in storage
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is good about a doubly linked list?

A

Doubly Linked list

  • Allows us to traverse our list forwards/backwards
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a pointer?

A

Pointer is just a reference to another variable.

const obj1 = { a: true }

const obj2 = obj1;

both point to the same thing in memory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a linked list and some of its qualities?

A

Definition of Linked List:

Linked lists are data structures that represent a series of nodes where each node contains two pieces of information: the value of the node and a pointer/reference to the next node in the list. The beginning of the list is called the head and the node at the end of the list is referred to as the tail, which points to the empty value; null.

notes

  • nodes are scattered
  • iterating through it is slower
  • INSERTing into it is a lot better (no shifting of the arrays)
  • you can sort data
  • prepend O(1)
  • append O(1)
  • lookup O(n) aka Traversal - we have to start from Head
  • insert O(n) - Have to go one by one to find the index and insert there
  • delete O(n) - Have to find the items
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a tradeoff for Hash tables?

A

Tradeoffs of Hash tables

  • Fast (better time complexity) but adds more memory aka Space complexity
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are some differences between Hash tables and Arrays?

A

Hash tables vs Arrays

Hash tables

  • when u want quick access to a certain item [search O(1)]
  • no concept of order
  • flexible keys
  • fast inserts
  • good collision resolution needed***

Bad: unordered and slow key iteration

Arrays

  • not so quick for search O(n)
  • each item is placed on a shelf next to each other in memory
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Difference between a Map vs. Object?

A

Map vs. Object

  • Map allows you to store any type of data as a key
  • Object only allows strings as keys
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the Big O of

‘asdfasfasfasfdasdfa’.length?

A

Depends on the language you are using. JavaScript has .length built in to each string.. so its a property of an object. so in JS it’ll be O(1) constant time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What adds to Space Complexity?

A

For space complexity, we don’t care about the input size.

What takes up space?

  • Variables
  • Data Structures ( like creating a new array and adding items to it)
  • function calls
  • allocations
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What to consider when thinking of Space Complexity?

A

Space Complexity:

  • Heap - store variables
  • Stack - keep track of functions calls
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What to consider when thinking of Scalability?

A

Scalability:

  • Speed
  • Memory
17
Q

What is the Big O of nested loops?

A

If you see nested loops, use multiplication to determine Big O

O(n^2) -> Quadratic Time

18
Q

What are the rules of Big O?

A

Big O Rules:

1) Worst Case
2) Remove Constants
3) Different terms for inputs
4) Drop Non Dominants

19
Q

What is Constant Time?

A

O(1) -> Constant Time (Grabbing the first item in the array)

the length of data doesn’t matter.

const compressFirstBox = boxes => {

console.log(boxes[0]);

}

20
Q

What is Linear Time?

A

O(n) -> Linear Time

21
Q

What makes good code?

A

1) Readable

2) Scalable (Big O)

22
Q

Whats the difference between Single threaded and Multi threaded?

A
  • Single threaded = Has only ONE call stack (FIFO) one thing at a time
  • Multi threaded = Has MORE than one call stack
23
Q

Whats the difference between Asynchronous and Synchronous programming?

A

Synchronous programming is when lines of code get executed one at a time

Asynchronous programming (image processing, api calls) - multiple things can happen at different times

  • setTimeout()
  • callback functions
24
Q

What is Stack overflow and what can cause it?

A

Stack Overflow:

  • When the call stack runs out of space
  • Caused by infinite loop or Recursion - a function that calls itself
25
Q

What will the console print out?

console.log(‘1’);

setTimeout(()=> {

console.log(‘2’);

}, 0);

console.log(‘3’);

A

Because we are still using the setTimeout api, it still goes through the process of the web api, then the callback queue, then the event loop.. only after console.log(‘3’) will it print ‘2’.