Design a Number Container System - Problem

Design a number container system that can perform the following operations:

  • Insert or Replace a number at the given index in the system.
  • Return the smallest index for the given number in the system.

Implement the NumberContainers class:

  • NumberContainers() Initializes the number container system.
  • void change(int index, int number) Fills the container at index with the number. If there is already a number at that index, replace it.
  • int find(int number) Returns the smallest index for the given number, or -1 if there is no index that is filled by number in the system.

Input & Output

Example 1 — Basic Operations
$ Input: operations = ["NumberContainers", "change", "find", "change", "find", "find"], values = [[], [1, 10], [1], [2, 20], [1], [3]]
Output: [-1, -1, -1]
💡 Note: NumberContainers nc = new NumberContainers(); nc.change(1, 10); // container[1] = 10. nc.find(1) returns -1 (number 1 not found); nc.change(2, 20); // container[2] = 20. nc.find(1) returns -1 (number 1 still not found); nc.find(3) returns -1 (number 3 not found).
Example 2 — Multiple Indices Same Number
$ Input: operations = ["NumberContainers", "change", "change", "find", "change", "find"], values = [[], [1, 10], [3, 10], [10], [2, 10], [10]]
Output: [1, 1]
💡 Note: Multiple indices can have same number. System returns smallest index: indices 1 and 3 both have 10, so find(10) returns 1. After adding index 2 with 10, find(10) still returns 1 (smallest).
Example 3 — Replace Existing Value
$ Input: operations = ["NumberContainers", "change", "find", "change", "find"], values = [[], [1, 10], [10], [1, 20], [10]]
Output: [1, -1]
💡 Note: Initially container[1] = 10, so find(10) returns 1. Then change(1, 20) replaces the value, so find(10) returns -1 (no longer exists).

Constraints

  • 1 ≤ index, number ≤ 109
  • At most 105 calls to change and find

Visualization

Tap to expand
Number Container System INPUT Operations: ["NumberContainers", "change", "find", "change", "find", "find"] values: [[],[1,10],[1],...] Data Structures: idx2num 1 : 10 2 : 20 num2idx (heap) 10: [1] 20: [2] Min-Heap for number 10: 1 smallest idx ALGORITHM STEPS 1 change(index, num) Update idx2num[index]=num Push index to num2idx[num] 2 find(number) Get min-heap for number Return smallest valid idx 3 Lazy Deletion Pop stale indices from heap Check idx2num for validity 4 Return Result -1 if not found min index otherwise Execution Trace: change(1,10) --> idx2num[1]=10 find(10) --> return 1 change(2,20) --> idx2num[2]=20 find(10)-->1, find(3)-->-1 FINAL RESULT Final Container State: idx2num 1 --> 10 2 --> 20 num2idx 10 --> [1] 20 --> [2] Output Array: [null, null, 1, null, 1, -1] Results Breakdown: NumberContainers() --> null change(1,10) --> null find(10) --> 1 [OK] change(2,20) --> null find(10)-->1, find(3)-->-1 [OK] Key Insight: Two Hash Maps + Min-Heap: Use idx2num for O(1) index lookup and num2idx with min-heaps for O(log n) smallest index. Lazy deletion ensures stale indices are removed only when accessed, maintaining efficiency without immediate cleanup. Time: change O(log n), find O(log n) amortized | Space: O(n) for both maps and heaps combined. TutorialsPoint - Design a Number Container System | Two Hash Maps with Min-Heap Approach
Asked in
Google 15 Amazon 12 Microsoft 8
23.5K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen