Make folder names unique by adding the smallest free (k) suffix
Company: Okta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
Overview: Assign a unique name to each new folder by appending the smallest free (k) suffix whenever the requested name is already taken. It tests hash map design, remembering the next suffix per base name to avoid rescanning, and handling requested names that already look suffixed.
Constraints
- 1 <= names.length <= 5 * 10^4
- 1 <= names[i].length <= 20
- names[i] consists of lowercase English letters, digits, and/or round brackets.
Examples
Input: (['gta', 'gta(1)', 'gta', 'avalon'],)
Expected Output: ['gta', 'gta(1)', 'gta(2)', 'avalon']
Explanation: Source example 1 (4 names): the directly requested gta(1) holds suffix 1, so the second gta becomes gta(2).
Input: (['doc', 'doc(1)', 'doc', 'doc(1)'],)
Expected Output: ['doc', 'doc(1)', 'doc(2)', 'doc(1)(1)']
Explanation: Source example 2 (4 names): the second doc skips the taken doc(1), and the repeated doc(1) gets its own suffix, giving doc(1)(1).
Hints
- A name the system produces with a suffix is taken from then on, the same as a requested one. A later request for exactly that string collides with it.
- Folders are never deleted. Think about what that tells you about the smallest free suffix for the same requested name as more requests for it arrive.
- Treat each requested string literally. An ending like (1) or (01) inside a request is ordinary text and has no effect on how a suffix is chosen for some other name.