Week 12 Day 2 – Dynamic Arrays and Ring Buffers
I haven’t made any progress with my JS project since yesterday was jam packed with work and lectures. In addition, we had about 2-3 hours of algorithms homework at night, which basically brought me all the way to 12 AM of continuous work. Additionally, I couldn’t finish the homework last night since my brain checked out for the day, so I went and made some fixes to my resume and cover letter, that brought me all the way up to 2 AM. This is going to be my schedule for the next two weeks or so, since I do want to crank out projects and keep up with the rest of my cohort in regards to the job search curriculum, as well as the job search timeline.
Last night’s homework was pretty awesome. We expanded on some concepts we studied previously towards the beginning of the start of our cohort. Last night’s concepts were dynamic arrays and buffer rings. The point of these algorithms projects is to deepen our understanding of how these concepts work by implementing them from scratch.
In dynamic arrays, we really focused on building the array methods shift
, unshift
, push
and pop
. We built a StaticArray
class that simulated places in the computer’s physical memory, and the only operations you could do with the static array was set and get values via indices.
We built a DynamicArray
class on top of the StaticArray
class that would have the above array methods, and compared the time complexity of each method with that of the RingBuffer
class. To keep it short, through writing our own methods, for dynamic arrays, setting, getting, pushing and popping array methods are constant time in regards to time complexity, but shift and unshift are linear time complexity. This is because every time shift or unshift is executed, every element within the array needs to be shifted either forward or backwards, meaning there’s a full iteration and reassignment that occurs.
However, in ring buffers, as long as there’s space within array, shifting and unshifting are also constant time(amortized in the methods push and unshift), because the pointer of the starting index is moved around to simulate shifting and unshifting. This means that the start of the array that a user sees could have a starting index that actually shows up AFTER the rest of elements in the array in physical memory. If that’s a bit confusing, definitely google some images to get a better idea, there’s some really awesome ones out there.
Anyways, back to work! Today’s algorithms project is awesomely nostalgic: HashMaps and LRU Caches! Yeee!