CS Flashcards
What does the acronym LIFO mean?
Last-in-first-out
What methods are available on a Stack data structure?
-
.push()
- put something on top-
.pop()
- take something off the top - (Convenience but not required for a Stack)
.peek()
- look at what’s on top without taking it off
-
What must you do to access the value at an arbitrary point in a stack (not just the “top”)?
- Store the items you
pop
off somewhere- If you can destroy the stack, just keep popping off
- Searching the stack is O(n)
What does the acronym FIFO mean?
First-in-first-out
What methods are available on a Queue data structure?
-
.enqueue()
- Add something to the queue (by the nature of queues, to the end)-
.dequeue()
- Remove something from the queue (by the nature of queues, from the start) - There may or may not be a convenience method
.peek()
among others
-
What must you do to access the value at an arbitrary point in a queue (not just the “front”)?
.dequeue()
values until you reach the desired point
What are some examples of simple operations?
- Declaring/assigning a variable
- Accessing/assigning array by index
- Accessing/assigning object by property
- Single-step expressions (
i + 200
,i < 200
) - A truth test
What is the order of best-to-worst time complexities?
-
O(1)
- constant time (increasingn
has no effect)-
O(log n)
- logarithmic time (increasingn
increases simple operations bylog n
) -
O(n)
- linear time (increasingn
increases simple operations byn
) -
O(n log n)
- log-linear time -
O(n^2)
- quadratic time (increasingn
increases simple operations byn^2
) -
O(2^n)
- exponential time -
O(n!)
- factorial time
-
What time-complexities are generally considered bad? Good?
-
O(1)
, andO(log n)
are good-
O(n)
is acceptable -
O(n log n)
is ok - Everything above that is bad and should be used very carefully
-
How are linked lists different from an array?
- Items in a linked list can only be accessed by sequentially traversing the list. Arrays can directly access desired values.
- Arrays store memory continuously (in the same place) which allows index access to be done via a simple jump, giving O(1) complexity. Linked lists do not have to allocate memory ahead of time and items can be stored all around the memory.
- Because linked lists are just pointing to each other, prepending something takes O(1) time. If there is a tail pointer, appending something is also O(1) time. Otherwise, appending something in the middle or at the end takes O(n) time.
How would you access an arbitrary node in a linked list (not just the “head”)?
Repeatedly access the next node in the list until you reach the desired one
How can you construct a stack with a linked list?
Stacks can add to the end and remove from the end
- Iterate through linked list until you get to the last - Add: Set the next to the new value - Remove: Keep track of the previous node, then once at the last node go back to the previous and remove that node's next
How can you construct a queue with a linked list?
Queues can add to the end and remove from the front
- Remove: Simply grab the head's next value, that is the head of the linked list where the first node has been dequeued - Add: Iterate to the end and set the last node's next to the new value
What must the return value ofmyFunction
be if the following expression is possible?
```js myFunction()(); ```
It must return a function
What does this code do?
js const wrap = value => () => value;
Creates a function factory wrap
that takes one parameter value
as its argument. The returned function takes no arguments but will return the argument for wrap
’s value
.