Cracking The Coding Interview/Q 3.1

From Software Engineers Wiki
Jump to: navigation, search

Describe how you could use a single array to implement three stacks.

Answer

Divide the array into three parts initially. The first stack grows from the beginning, and the second stack grows from the end. The third stack starts from 1/3 of the array, and grows toward end of the array. When the middle stack collides with other stacks, and if space problem can be solved by moving the stack around, move the stack. If the entire array is used up, reallocate memory and redistribute the stack.

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox