27.10.2014 Views

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Solutions</strong> to Chapter 3 | Stacks <strong>and</strong> Queues<br />

3.3 Imagine a (literal) stack of plates. If <strong>the</strong> stack gets too high, it might topple. Therefore,<br />

in real life, we would likely start a new stack when <strong>the</strong> previous stack exceeds<br />

some threshold. Implement a data structure SetOfStacks that mimics this. SetOf-<br />

Stacks should be composed of several stacks, <strong>and</strong> should create a new stack once<br />

<strong>the</strong> previous one exceeds capacity. SetOfStacks.push() <strong>and</strong> SetOfStacks.pop() should<br />

behave identically to a single stack (that is, pop() should return <strong>the</strong> same values as it<br />

would if <strong>the</strong>re were just a single stack).<br />

FOLLOW UP<br />

Implement a function popAt(int index) which performs a pop operation on a specific<br />

sub-stack.<br />

SOLUTION<br />

In this problem, we’ve been told what our data structure should look like:<br />

1 class SetOfStacks {<br />

2 ArrayList stacks = new ArrayList();<br />

3 public void push(int v) { ... }<br />

4 public int pop() { ... }<br />

5 }<br />

pg 52<br />

We know that push() should behave identically to a single stack, which means that we need<br />

push() to call push on <strong>the</strong> last stack. We have to be a bit careful here though: if <strong>the</strong> last stack<br />

is at capacity, we need to create a new stack. Our code should look something like this:<br />

1 public void push(int v) {<br />

2 Stack last = getLastStack();<br />

3 if (last != null && !last.isAtCapacity()) { // add to last stack<br />

4 last.push(v);<br />

5 } else { // must create new stack<br />

6 Stack stack = new Stack(capacity);<br />

7 stack.push(v);<br />

8 stacks.add(stack);<br />

9 }<br />

10 }<br />

What should pop() do? It should behave similarly to push(), in that it should operate on <strong>the</strong><br />

last stack. If <strong>the</strong> last stack is empty (after popping), <strong>the</strong>n we should remove it from <strong>the</strong> list<br />

of stacks.<br />

1 public int pop() {<br />

2 Stack last = getLastStack();<br />

3 int v = last.pop();<br />

4 if (last.size == 0) stacks.remove(stacks.size() - 1);<br />

5 return v;<br />

6 }<br />

1 1 5<br />

<strong>Cracking</strong> <strong>the</strong> <strong>Coding</strong> <strong>Interview</strong> | Data Structures

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!