Wrapping Around Six Slots
Open Data Structures implements ArrayDeque over a circular array with a backing array a, a head index j and a count n. Element i of the deque lives at a[(j + i) % a.length], and the single method add(i, x) shifts whichever side is shorter. Call its two end cases addFirst and addLast. Adding at the front runs j = (j == 0) ? a.length - 1 : j - 1 and then writes a[j] = x; adding at the back writes a[(j + n) % a.length] = x. Neither end case shifts anything.
Start from this state and run addLast(1), addLast(2), addLast(3), addLast(4), addFirst(9), addFirst(8).
What are the physical contents of a[0..5] and the value of j at the end?
Sign in to answer questions and track your progress
Sign In