Design 3 Stacks in 1 Array

Quick Overview

Implement three independent stacks inside one fixed-size array, with push, pop, peek and isEmpty on each stack. Tests index arithmetic over array segments, per-stack size tracking, and correct handling of full and empty stacks in constant time per operation.

Design 3 Stacks in 1 Array

Company: Intuit

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

Design a data structure that implements **three independent stacks** using a **single fixed-size array**. The underlying array has a total capacity of `3 * stackSize`, divided equally so that each stack can hold at most `stackSize` elements. Stack IDs are `0`, `1`, and `2`. The constructor `TripleStack(stackSize)` creates the three stacks, all empty. If a `push` is attempted on a full stack, do nothing. If a `pop` or `peek` is attempted on an empty stack, return `-1`. Implement the following methods: - `push(stackId: int, value: int)`: Push a value onto the stack identified by stackId (0, 1, or 2). If the stack is full, do nothing. - `pop(stackId: int) -> int`: Pop and return the top value from the stack identified by stackId. Return -1 if the stack is empty. - `peek(stackId: int) -> int`: Return the top value of the stack identified by stackId without removing it. Return -1 if the stack is empty. - `isEmpty(stackId: int) -> bool`: Return true if the stack identified by stackId is empty, false otherwise. Example 1: ```text Input: ["TripleStack", "push", "push", "push", "peek", "peek", "pop", "isEmpty", "peek"] [[1], [0, 7], [0, 8], [2, 3], [0], [2], [2], [2], [1]] Output: [null, null, null, null, 7, 3, 3, true, -1] Explanation: ts = TripleStack(1) # each stack can hold 1 element ts.push(0, 7) # stack 0: [7] ts.push(0, 8) # stack 0 is full, push ignored (stack 0: [7]) ts.push(2, 3) # stack 2: [3] ts.peek(0) # -> 7 ts.peek(2) # -> 3 ts.pop(2) # -> 3 (stack 2 is now empty) ts.isEmpty(2) # -> true ts.peek(1) # -> -1 (stack 1 was never used) ``` Example 2: ```text Input: ["TripleStack", "pop", "peek", "isEmpty", "push", "push", "pop", "pop", "isEmpty"] [[2], [0], [1], [2], [0, 5], [0, 10], [0], [0], [0]] Output: [null, -1, -1, true, null, null, 10, 5, true] Explanation: ts = TripleStack(2) # each stack can hold up to 2 elements ts.pop(0) # -> -1 (stack 0 is empty) ts.peek(1) # -> -1 (stack 1 is empty) ts.isEmpty(2) # -> true (stack 2 is empty) ts.push(0, 5) # stack 0: [5] ts.push(0, 10) # stack 0: [5, 10] ts.pop(0) # -> 10 (stack 0: [5]) ts.pop(0) # -> 5 (stack 0 is now empty) ts.isEmpty(0) # -> true (stack 0 is empty) ``` ```hint Where the top lives Work out which array index holds the top of stack `i` when it currently has `k` elements. Watch the off-by-one between "next free slot" and "current top". ``` ### Constraints & Assumptions - `1 <= stackSize <= 1000` - `0 <= stackId <= 2` - `0 <= value <= 10⁴`, so `-1` can never be a real stored value. - At most `10⁴` calls total will be made to `push`, `pop`, `peek`, and `isEmpty`. ### What a Strong Answer Covers - How each stack's region of the array is located from its `stackId` - What per-stack state is kept and how it changes on push and pop - The full-stack and empty-stack behavior exactly as specified, with no stack writing into another's region - Time per operation and total space ### Follow-up Questions - With a fixed split, one stack can be full while the other two are empty. How would you let a stack use free space anywhere in the array? - How would you generalize the design to `k` stacks sharing one array of size `n`? - If values could be negative, how would you change the API so an empty pop is distinguishable from a real value? - What changes if several threads call these methods at once?

Overview: Implement three independent stacks inside one fixed-size array, with push, pop, peek and isEmpty on each stack. Tests index arithmetic over array segments, per-stack size tracking, and correct handling of full and empty stacks in constant time per operation.

Intuit logo
Intuit
Nov 24, 2026
easySoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a data structure that implements three independent stacks using a single fixed-size array.

The underlying array has a total capacity of 3 * stackSize, divided equally so that each stack can hold at most stackSize elements. Stack IDs are 0, 1, and 2. The constructor TripleStack(stackSize) creates the three stacks, all empty.

If a push is attempted on a full stack, do nothing. If a pop or peek is attempted on an empty stack, return -1.

Implement the following methods:

  • push(stackId: int, value: int) : Push a value onto the stack identified by stackId (0, 1, or 2). If the stack is full, do nothing.
  • pop(stackId: int) -> int : Pop and return the top value from the stack identified by stackId. Return -1 if the stack is empty.
  • peek(stackId: int) -> int : Return the top value of the stack identified by stackId without removing it. Return -1 if the stack is empty.
  • isEmpty(stackId: int) -> bool : Return true if the stack identified by stackId is empty, false otherwise.

Example 1:

Input:
["TripleStack", "push", "push", "push", "peek", "peek", "pop", "isEmpty", "peek"]
[[1],           [0, 7], [0, 8], [2, 3], [0],    [2],    [2],   [2],       [1]]

Output: [null, null, null, null, 7, 3, 3, true, -1]

Explanation:
ts = TripleStack(1)      # each stack can hold 1 element
ts.push(0, 7)            # stack 0: [7]
ts.push(0, 8)            # stack 0 is full, push ignored (stack 0: [7])
ts.push(2, 3)            # stack 2: [3]
ts.peek(0)               # -> 7
ts.peek(2)               # -> 3
ts.pop(2)                # -> 3    (stack 2 is now empty)
ts.isEmpty(2)            # -> true
ts.peek(1)               # -> -1   (stack 1 was never used)

Example 2:

Input:
["TripleStack", "pop", "peek", "isEmpty", "push", "push",  "pop", "pop", "isEmpty"]
[[2],           [0],   [1],    [2],       [0, 5], [0, 10], [0],   [0],   [0]]

Output: [null, -1, -1, true, null, null, 10, 5, true]

Explanation:
ts = TripleStack(2)      # each stack can hold up to 2 elements
ts.pop(0)                # -> -1   (stack 0 is empty)
ts.peek(1)               # -> -1   (stack 1 is empty)
ts.isEmpty(2)            # -> true (stack 2 is empty)
ts.push(0, 5)            # stack 0: [5]
ts.push(0, 10)           # stack 0: [5, 10]
ts.pop(0)                # -> 10   (stack 0: [5])
ts.pop(0)                # -> 5    (stack 0 is now empty)
ts.isEmpty(0)            # -> true (stack 0 is empty)

Constraints & Assumptions

  • 1 <= stackSize <= 1000
  • 0 <= stackId <= 2
  • 0 <= value <= 10⁴ , so -1 can never be a real stored value.
  • At most 10⁴ calls total will be made to push , pop , peek , and isEmpty .

What a Strong Answer Covers Guidance

  • How each stack's region of the array is located from its stackId
  • What per-stack state is kept and how it changes on push and pop
  • The full-stack and empty-stack behavior exactly as specified, with no stack writing into another's region
  • Time per operation and total space

Follow-up Questions Guidance

  • With a fixed split, one stack can be full while the other two are empty. How would you let a stack use free space anywhere in the array?
  • How would you generalize the design to k stacks sharing one array of size n ?
  • If values could be negative, how would you change the API so an empty pop is distinguishable from a real value?
  • What changes if several threads call these methods at once?