Separate-Chaining Hash Table
Updated
Problem statement
Build a hash table that maps integer keys to integer values using separate chaining. The table starts empty and has bucketCount buckets. A key goes to bucket ((key % bucketCount) + bucketCount) % bucketCount. Each bucket keeps its own collection of key/value entries. Do not use a built-in map or dictionary to build the table.
Go through operations in order and return one integer for each operation:
[0, key, value]: put the value under the key. Return1if the key is new, and0if the key was already there and its value was overwritten.[1, key]: get the key's current value, or return-1if the key is not in the table.[2, key]: remove the key. Return1if it was there, and0otherwise.
Different keys in the same bucket must all be kept. Overwriting a key does not add a second entry. The number of buckets never changes.
Function
processChainedHashTable(bucketCount: int, operations: int[][]) → int[]Examples
Example 1
bucketCount = 2operations = [[0,1,10],[0,3,30],[1,1],[0,1,12],[1,1],[2,3],[1,3]]return = [1,1,10,0,12,1,-1]Keys 1 and 3 land in the same bucket, but each one keeps its own entry, so both of the first puts report a new key. The later put for key 1 only overwrites its value and adds no second entry. Every get then returns the latest value, and once the second key is removed, a get for it finds nothing.
Example 2
bucketCount = 2operations = [[0,-1,5],[0,1,8],[1,-1],[2,-1],[1,1]]return = [1,1,5,1,8]The negative key goes through the bucket formula and lands in the same bucket as the positive key. Both puts report a new key. The get returns the negative key's value, and then that key is removed. The positive key in the same bucket is still there, so the last get still returns its value.
Example 3
bucketCount = 5operations = [[1,42],[2,42]]return = [ -1,0 ]The table is empty. The get finds no such key and returns -1. The remove has nothing to delete, so it returns 0.
Constraints
1 <= bucketCount <= 1000.0 <= operations.length <= 10000.- Every operation has one of the three forms above.
-1000000 <= key <= 1000000.0 <= value <= 1000000.
More Microsoft Onsite Interview questions
- Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Rectangle Point Coverage CountsOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Transactional Key-Value StoreGrammarly · ONSITE INTERVIEW
- LRU CacheApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
- LRU Cache for Query ResultsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Highest Bid Campaign MatcherStackAdapt · ONSITE INTERVIEWStackAdapt interview questions
- Random Dasher Set OperationsDoorDash · ONSITE INTERVIEWDoorDash interview questions
- File System Shell CommandsHarvey · ONSITE INTERVIEWHarvey interview questions
- Timed Cache with Sidecar CleanupNetflix · PHONE SCREEN · ONSITE INTERVIEWNetflix interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- LRU CacheServiceNow · PHONE SCREEN · ONSITE INTERVIEWServiceNow interview questions