# Algorithms and Data Structures Interview Preparation & Walkthrough — Part 2, Array and String In my previous post: Algorithms and Data Structures Interview Preparation & Walkthrough — Part 1, we talked about how to do Complexity Time and Space analysis, and also see the common Big-O factors with examples.

In this post, I will start talking about Array in depth, and cover some interview questions and hopefully by the end of the reading, you would have a good glance about Array. Once we are familiar with Array, I will talk about String and how to use Array to solve String problems.

Array is a data structure that contains a group of elements. The most basic implementation of an array is a static array. The reason it’s called static is the size is fixed. The read/write access to a certain position is O(1).

#### Implementation of Static Array

`class StaticArraydef initialize(length)self.store = Array.new(length)end`
`# O(1)def [](index)self.store[index]end`
`# O(1)def []=(index, value)self.store[index] = valueend`
`protectedattr_accessor :storeend`

We create a Dynamic Array from Static Array as follow. The read/write access is also O(1). Let’s implement some general methods i.e. pop(), push(), shift() and unshift() for it. The key here is, when we reach the array size, we want to resize it and double its space, in order to push() or unshift() new elements to the array.

#### Implementation of Dynamic Array

`require_relative "static_array"`
`class DynamicArrayattr_reader :length`
`def initialize@length = 0@capacity = 8@store = StaticArray.new(8)end`
`# O(1)def [](index)check_index(index)@store[index]end`
`# O(1)def []=(index, value)check_index(index)@store[index] = valueend`
`# O(1)def popcheck_index(0)@length -= 1@store[length + 1]end`
`# O(1) amortized; O(n) worst case.def push(val)resize! if @length == @capacity@store[@length + 1] = val@length += 1end`
`# O(n): has to shift over all the elements.def shiftcheck_index(0)idx = 0first_el = @storewhile idx < @length - 1@store[idx] = @store[idx + 1]idx += 1end@length -= 1first_elend`
`# O(n): has to shift over all the elements.def unshift(val)resize! if @length == @capacityidx = @lengthwhile idx > 0@store[idx] = @store[idx - 1]idx -= 1end@store = val@length += 1@storeend`
`protectedattr_accessor :capacity, :storeattr_writer :length`
`def check_index(index)raise "out of bounds" if (@length < index + 1 || index < 0)end`
`# O(n): has to copy over all the elements to the new store.def resize!new_store = StaticArray.new(@capacity * 2)idx = 0while idx < @lengthnew_store[idx] = @store[idx]idx += 1end@store = new_store@capacity *= 2endend`

#### What is amortization?

If you read carefully enough, you would notice there is a keyword “amortized” in the code snippet. What does that mean? When we want to append (or push) a new element to the Array and it reaches its size limit, we want to double the size. However, `resize!` method allocates a larger region, moves the whole array, and deletes the previous. This is a `O(n)` operation. But if we’re only doing it every `O(1/n)` times, then on average it can still come out to `O(n * 1/n) = O(1)`. That’s called amortized cost.

#### Time Complexity and Space Complexity for Dynamic Array

In average and worst cases,

`Access O(1)`

`Search O(n)`

`Insertion O(n)`(at the end of Array is O(1) amortized, at the beginning or middle of Array is O(n)

`Deletion O(n)`

`Space O(n)`

We now know accessing an element in Array is fast (`O(1)`), whereas searching/adding/removing is relatively slow (`O(n)`), which sometimes requires looping through the whole array.

#### Ring Buffer

It is a data structure that uses a Static Array as if it were connected end-to-end.

`require_relative "static_array"`
`class RingBufferattr_reader :length`
`def initialize@length = 0@capacity = 8@start_idx = 0@store = StaticArray.new(@capacity)end`
`# O(1)def [](index)check_index(index)ring_index = (index + @start_idx) % @capacity@store[ring_index]end`
`# O(1)def []=(index, val)check_index(index)ring_index = (index + @start_idx) % @capacity@store[ring_index] = valend`
`# O(1)def popcheck_index(0)@length -= 1;val = @store[(@length + @start_idx) % @capacity]@store[(@length + @start_idx) % @capacity] = nilvalend`
`# O(1) amortizeddef push(val)resize! if @length == @capacity@store[(@length + @start_idx) % @capacity] = val@length += 1end`
`# O(1)def shiftcheck_index(0)val = @store[@start_idx]@store[@start_idx] = nil@start_idx = (@start_idx + 1) % @capacity@length -= 1valend`
`# O(1) amortizeddef unshift(val)resize! if @length == @capacity@start_idx = (@start_idx - 1) % @capacity@store[@start_idx] = val@length += 1valend`
`protectedattr_accessor :capacity, :start_idx, :storeattr_writer :length`
`def check_index(index)raise "index out of bounds" if (index < 0 || index > @length - 1)end`
`def resize!new_store = StaticArray.new(@capacity * 2)idx = 0while idx < @lengthnew_store[idx] = @store[(@start_idx + idx) % @capacity]idx += 1end@store = new_store@start_idx = 0@capacity *= 2endend`

To master Array data structure and questions, we at least need to be very familiar with:

1. Loop operation.
2. Sense of using pointer(s) to record location(s).
3. Swap technique.
4. Basic Math.
5. Common array methods and the time complexity of them, i.e. pop(), push(), shift(), unshift(), forEach(), sort(), slice(), splice(), reverse(), concat(), filter(), map() … etc.

● Constant time access and allows random access

● Grouping like items

1. Move Zeros — Given an array of numbers, write a function to move all `0`‘s to the end of it while maintaining the relative order of the non-zero elements. (thought process and solution)