The Design a Number Container System interview question requires implementing a system that associates indices with numbers. You can insert or replace a number at a specific index and query the smallest index that currently contains a specific number. This Design a Number Container System coding problem is a classic "Two-Way Mapping" challenge where you need to look up a value by its key and a key by its value.
Amazon and Google ask this to evaluate your ability to maintain Bidirectional Relationships between data points. It tests your proficiency with Hash Table interview patterns for the index-to-number mapping and Ordered Set interview patterns for the number-to-indices mapping. It’s about ensuring consistency across two different internal views of the same data.
This problem uses two primary maps:
Map<Integer, Integer> indexToNum: To store which number is at which index.Map<Integer, TreeSet<Integer>> numToIndices: For each number, maintain a sorted set of all indices where it appears.change(index, number) is called:
index already had a number. If so, remove index from the old number's TreeSet.indexToNum with the new number.index to the new number's TreeSet.change(10, 5): indexToNum[10]=5, numToIndices[5]={10}.change(2, 5): indexToNum[2]=5, numToIndices[5]={2, 10}.find(5): The smallest index in numToIndices[5] is 2. Return 2.change(2, 3): numToIndices[5] becomes {10}, numToIndices[3] becomes {2}.TreeSet or std::set.For any problem that asks for "the smallest/largest key associated with a value," you need a sorted structure (like a Heap or BST) indexed by that value. If you need to remove specific keys from that sorted structure, a BST-based Set is usually better than a Heap.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Design Task Manager | Medium | Solve | |
| Smallest Number in Infinite Set | Medium | Solve | |
| Stock Price Fluctuation | Medium | Solve | |
| Exam Room | Medium | Solve | |
| Design Movie Rental System | Hard | Solve |