Design a Number Container System - Problem
Design a Number Container System

You need to design a smart container system that can efficiently manage numbers at specific indices. Think of it as a dynamic array where you can:

🔧 Insert or Replace: Place any number at any index, replacing existing values if needed
🔍 Quick Lookup: Find the smallest index that contains a specific number

Your system should support two main operations:

change(index, number) - Updates the container at the given index with the new number
find(number) - Returns the smallest index containing that number, or -1 if not found

The challenge is to make both operations as efficient as possible, especially when dealing with frequent updates and lookups.

Example:
NumberContainers nc = new NumberContainers();
nc.change(1, 10); // Container[1] = 10
nc.change(2, 20); // Container[2] = 20
nc.change(3, 10); // Container[3] = 10
nc.find(10); // Returns 1 (smallest index with value 10)
nc.change(1, 30); // Container[1] = 30
nc.find(10); // Returns 3 (now smallest index with 10)

Input & Output

example_1.py — Basic Operations
$ Input: ["NumberContainers", "find", "change", "change", "find", "change", "find"] [[], [10], [2, 10], [1, 10], [10], [3, 10], [10]]
Output: [null, -1, null, null, 1, null, 1]
💡 Note: Initially empty, find(10) returns -1. After change(2,10) and change(1,10), find(10) returns 1 (smallest index). After change(3,10), find(10) still returns 1.
example_2.py — Index Updates
$ Input: ["NumberContainers", "change", "change", "change", "find", "change", "find"] [[], [1, 10], [2, 20], [3, 10], [10], [1, 30], [10]]
Output: [null, null, null, null, 1, null, 3]
💡 Note: Initially containers: {1:10, 2:20, 3:10}. find(10) returns 1. After changing index 1 to 30, find(10) returns 3.
example_3.py — Edge Case Empty
$ Input: ["NumberContainers", "find", "change", "find", "change", "find"] [[], [1], [1, 10], [1], [1, 20], [10]]
Output: [null, -1, null, -1, null, -1]
💡 Note: find(1) returns -1 initially and after change(1,10). After change(1,20), find(10) returns -1 since 10 no longer exists.

Constraints

  • 1 ≤ index, number ≤ 109
  • At most 105 calls will be made in total to change and find
  • Each operation must be handled efficiently

Visualization

Tap to expand
Library ShelvesShelf 1: Book AShelf 2: Book BShelf 3: Book AShelf Catalog1 → Book A2 → Book B3 → Book A5 → Book CBook IndexBook A: [1,3]Book B: [2]Book C: [5]13Min-Heap"Where is Book A?" → Check index → Return shelf 1
Understanding the Visualization
1
Book Catalog
Maintain a catalog mapping shelf numbers to books stored there
2
Location Index
Keep an index mapping each book title to all shelf locations, sorted by shelf number
3
Quick Updates
When moving a book, update both the catalog and location index
4
Fast Lookups
To find a book, check the location index for the lowest shelf number
Key Takeaway
🎯 Key Insight: By maintaining both a shelf catalog (index→number) and a book location index (number→min-heap), we can perform both updates and lookups efficiently!
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
42.5K Views
High Frequency
~25 min Avg. Time
1.3K 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