Design a Set of Stacks with `popAt`

Data Structures
Hard
Adobe
103.1K views

Implement a `SetOfStacks` data structure where a new stack is created once the previous one exceeds capacity. Implement a `popAt` operation that performs a pop on a specific sub-stack.

Why Interviewers Ask This

Adobe evaluates this question to assess a candidate's ability to design scalable data structures that handle memory fragmentation and edge cases. It tests understanding of array-based stack implementations, pointer manipulation across sub-structures, and the critical trade-off between strict capacity enforcement versus maintaining structural integrity during partial pops.

How to Answer This Question

1. Clarify requirements: Ask if popAt should shift elements from subsequent stacks to maintain balance or simply remove from the target index. 2. Define core structure: Propose an ArrayList or List of Stacks where each internal stack has a fixed capacity. 3. Implement push logic: Explain how to check if the current top stack is full before adding; if full, instantiate a new stack. 4. Design popAt logic: Detail retrieving the specific sub-stack, performing the pop, and handling the case where the popped stack becomes empty (optional cleanup). 5. Discuss complexity: Analyze time complexity for push (O(1)) and popAt (O(N) worst-case if shifting required), noting Adobe values efficiency in high-throughput environments like Creative Cloud.

Key Points to Cover

  • Explicitly defining the behavior when a sub-stack becomes empty after a pop
  • Clarifying whether elements must be shifted to maintain fullness constraints
  • Demonstrating awareness of O(1) vs O(N) trade-offs in the popAt implementation
  • Handling boundary conditions like invalid indices or empty collections gracefully
  • Connecting the solution to real-world scalability needs typical of Adobe products

Sample Answer

To solve this, I would first define a SetOfStacks class containing a list of individual Stack objects, each with a defined capacity. For the push operation, I'd check if the last stack in the list has reached its limit.…

Common Mistakes to Avoid

  • Failing to ask about the shifting requirement, leading to an incorrect assumption about data movement
  • Implementing a single large array instead of a list of lists, missing the 'Set' concept entirely
  • Ignoring edge cases where the requested index exceeds the number of available sub-stacks
  • Not considering the memory overhead of creating too many small stack objects without a threshold

Sound confident on this question in 5 minutes

Answer once and get a 30-second AI critique of your structure, content, and delivery. First attempt is free — no signup needed.

Try it free

Related Interview Questions

Browse all 166 Data Structures questionsBrowse all 25 Adobe questions