Arrays Flashcards

1
Q

How do dynamic arrays allocate extra space in ruby?

A

Remember that Ruby is a wrapper for C, which has static arrays. Each time there is not enough space in the array, double the current size of the array, reallocate memory, and copy over the elements currently stored in the array.

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

How does #unshift work? Explain its time complexity (ruby)

A

Instead of copying over every element and shifting them right one, It uses a ring buffer to accomplish this in O(1)..

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

Explain a ring buffer

A

Circular array. In addition to keeping track of the length of the stored data, the location, and capacity, the ring buffer also keeps track of start location

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