Microsoft Interview Question
SDE-2sCountry: United States
Use hash-table to store strings as string-length as key and list of strings as value. When request comes for a given string length it gets it directly from hash-table in O(n) time. If the list of string pointed by this hash key is empty then system generates a new random string and gives it to client. Assuming that client will return back this once done we wouldn’t save it in hash table now. Once client returns this string we add it to list of appropriate hash key (determined by string length).
You can use a hashtable with strings as a key and an inUse boolean as a value.
- SK January 23, 2015When clients request a string, they are put in a queue to use the string if its inUse value is found to be "true".
It's basically a kind of manual implementation of mutex in multithreading