Bird
Raised Fist0
Interview Prepcustom-data-structuresmediumAmazonGoogle

Design a Stack With Increment Operation

Choose your preparation mode4 modes available
▶
Steps
setup

Initialize CustomStack with maxSize 3

Create an empty stack and an increments array of size 3 initialized to zeros. The stack is empty and ready to accept elements.

💡 Initialization sets up the data structures needed for efficient push, pop, and increment operations.
Line:def __init__(self, maxSize: int): self.stack = [] self.inc = [0] * maxSize self.maxSize = maxSize
💡 The increments array will track lazy increments for stack elements by index.
📊
Design a Stack With Increment Operation - Watch the Algorithm Execute, Step by Step
Watching each operation step-by-step reveals how lazy increments work and how the stack and increments array interact, which is difficult to grasp from code alone.
Step 1/13
·Active fill★Answer cell
setup
insert
1
insert
1
→
2
delete
1
Result: 2
insert
1
→
2
insert
1
→
2
→
3
insert
1
→
2
→
3
expand
1
→
2
→
3
expand
1
→
2
→
3
delete
1
→
2
Result: 103
delete
1
Result: 202
delete
Result: 201
delete
Result: -1

Key Takeaways

✓ Lazy increments allow efficient increment operations without updating all elements immediately.

This insight is hard to see from code alone because the increments array is separate and propagation happens only on pop.

✓ Pop operation not only removes the top element but also propagates increments down the stack.

Visualizing this propagation clarifies how increments accumulate and affect subsequent pops.

✓ Push respects maxSize and ignores pushes when full, preventing overflow.

Seeing the stack size visually helps understand capacity constraints and why some pushes are ignored.

Practice

(1/5)
1. You need to design a data structure that supports adding intervals, removing intervals, and querying whether a given range is fully covered by the added intervals. Which approach guarantees efficient updates and queries with logarithmic time complexity per operation?
easy
A. Use a balanced binary search tree (e.g., TreeMap) to store and merge intervals, enabling logarithmic time operations.
B. Use a brute force list of intervals and scan all intervals for each operation.
C. Use dynamic programming to precompute coverage for all possible ranges.
D. Use a greedy algorithm that always merges intervals only when queried.

Solution

  1. Step 1: Understand the problem requirements

    The data structure must support add, remove, and query operations on intervals efficiently.
  2. Step 2: Evaluate approaches

    Brute force scanning is O(n) per operation, which is inefficient. DP is not suitable for dynamic interval updates. Greedy merging only on query delays merging and leads to inefficient queries. Balanced BST with interval merging maintains sorted intervals and merges overlapping ones, supporting O(log n) operations.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Balanced BST with merging -> O(log n) per operation [OK]
Hint: Balanced BST with merging enables efficient interval operations [OK]
Common Mistakes:
  • Thinking DP or greedy solves dynamic interval updates efficiently
2. What is the average time complexity of the remove operation in the optimized Insert Delete GetRandom O(1) with duplicates data structure that uses a hash map of element to linked list nodes and a list of elements?
medium
A. O(n) because we need to find the element's index in the list before removal.
B. O(log n) due to maintaining the linked list structure during removals.
C. O(1) worst-case because linked list removals and hash map lookups are constant time.
D. O(1) average because the hash map provides direct access to an element's node for removal.

Solution

  1. Step 1: Identify data structure operations

    Removal uses hash map to get a node reference in O(1) average time, then removes node from doubly linked list in O(1).
  2. Step 2: Clarify average vs worst-case

    Hash map lookups are average O(1), but worst-case can degrade. Linked list removal is always O(1). So overall average is O(1).
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Hash map + linked list enables average O(1) removals [OK]
Hint: Hash map + linked list enables average O(1) removals [OK]
Common Mistakes:
  • Confusing average and worst-case complexities
  • Assuming linked list removal is O(log n)
  • Thinking index search is needed
3. Suppose the linked list with random pointers can contain cycles formed by random pointers (i.e., random pointers may create cycles independent of next pointers). Which approach correctly copies such a list without infinite loops or duplicate nodes?
hard
A. Use the optimal weaving approach as is, since it handles all cases in O(1) space.
B. Use a recursive approach with memoization to track already copied nodes and avoid infinite recursion.
C. Use the brute force approach but skip assigning random pointers to avoid cycles.
D. Modify the optimal approach to break cycles by removing random pointers before copying.

Solution

  1. Step 1: Understand cycle implications

    Random pointer cycles cause infinite loops if naive traversal is used without tracking visited nodes.
  2. Step 2: Identify safe approach

    Recursive copying with memoization tracks visited nodes, preventing infinite recursion and duplicate copies.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Memoization ensures each node is copied once even with cycles [OK]
Hint: Memoization prevents infinite recursion in cyclic graphs [OK]
Common Mistakes:
  • Assuming weaving approach handles cycles safely
  • Ignoring cycles and causing infinite loops
  • Removing random pointers loses information
4. Suppose you want to modify the skiplist to support duplicate values and allow searching for the k-th occurrence of a target. Which modification is the most appropriate to maintain efficient search and insertion?
hard
A. Insert duplicate nodes at the lowest level only, ignoring higher levels
B. Store a count of duplicates in each node and update counts on insert/erase
C. Use a hash map to track occurrences separately from the skiplist
D. Disallow duplicates and reject insertions of existing values

Solution

  1. Step 1: Understand duplicate handling

    To support duplicates and k-th occurrence search, nodes must track counts or occurrences.
  2. Step 2: Evaluate options

    Storing counts in nodes and updating them maintains skiplist structure and efficient operations.
  3. Step 3: Reject other options

    Inserting duplicates only at lowest level breaks skiplist invariants; hash map adds overhead; disallowing duplicates contradicts requirement.
  4. Final Answer:

    Option B -> Option B
  5. Quick Check:

    Counting duplicates in nodes preserves skiplist efficiency and supports k-th occurrence [OK]
Hint: Count duplicates in nodes to handle multiple occurrences efficiently [OK]
Common Mistakes:
  • Ignoring duplicates in higher levels
  • Using external structures breaking skiplist properties
  • Disallowing duplicates despite requirement
5. Suppose the skyline problem is extended so that buildings can overlap and have negative heights (e.g., representing underground structures). Which modification to the divide and conquer approach is necessary to correctly handle this variant?
hard
A. Modify the merge step to consider absolute values of heights when computing max height
B. Adjust the merge logic to track both max and min heights at each x-coordinate and merge accordingly
C. No modification needed; the existing merge logic works for negative heights
D. Replace divide and conquer with a brute force height map simulation to handle negative heights

Solution

  1. Step 1: Understand impact of negative heights

    Negative heights mean skyline can go below ground, so max height alone is insufficient to represent shape.
  2. Step 2: Modify merge logic accordingly

    Need to track both max and min heights at each x to correctly merge skylines with negative and positive heights.
  3. Step 3: Evaluate other options

    Using absolute values loses sign info; brute force is inefficient; no modification fails correctness.
  4. Final Answer:

    Option B -> Option B
  5. Quick Check:

    Tracking both max and min heights handles negative overlaps correctly [OK]
Hint: Negative heights require tracking min and max heights, not just max [OK]
Common Mistakes:
  • Ignoring negative heights
  • Using absolute values incorrectly
  • Assuming no change needed
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard