Quick 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.

Make folder names unique by adding the smallest free (k) suffix

Company: Okta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You create `n` folders in a file system, one per minute. At minute `i` you create a folder with the name `names[i]`. Two folders cannot have the same name. If you enter a folder name that was used before, the system adds a suffix of the form `(k)` to it, where `k` is the smallest positive integer such that the resulting name is still unique. Return an array `ans` of length `n` where `ans[i]` is the actual name the system assigns to the `i`th folder when you create it. ### Function Signature ```python def get_folder_names(names: list[str]) -> list[str]: ``` ### Rules - A name counts as used once it has been assigned to any earlier folder, whether it was requested as is or produced by adding a suffix. Folders are never deleted. - The suffixed name is the requested name followed by `(`, the decimal value of `k` with no leading zeros, and `)`. For example, `"doc"` with `k = 2` becomes `"doc(2)"`. - A requested name that already ends in something like `(1)` is treated as an ordinary string. If `"doc(1)"` is requested while it is taken, the new suffix goes after it, giving `"doc(1)(1)"`. ### 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 ```text Input: names = ["gy","jw","jw","jw","jw","jw"] Output: ["gy","jw","jw(1)","jw(2)","jw(3)","jw(4)"] ``` The first `"gy"` and the first `"jw"` are free. Each later `"jw"` takes the smallest suffix nobody has used yet, so the four repeats become `"jw(1)"` through `"jw(4)"`. ```text Input: names = ["gta","gta(1)","gta","avalon"] Output: ["gta","gta(1)","gta(2)","avalon"] ``` `"gta(1)"` is requested directly at minute 1. When `"gta"` comes again at minute 2, the suffix `(1)` is already taken, so the smallest free one is `(2)`. `"avalon"` is new. ```text Input: names = ["doc","doc(1)","doc","doc(1)"] Output: ["doc","doc(1)","doc(2)","doc(1)(1)"] ``` The second `"doc"` skips `(1)` because `"doc(1)"` exists and becomes `"doc(2)"`. The second request for `"doc(1)"` collides with the folder from minute 1, so it gets its own suffix and becomes `"doc(1)(1)"`.

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.

You create `n` folders in a file system, one per minute. At minute `i` you create a folder with the requested name `names[i]`. Two folders cannot have the same name. If you request a name that was used before, the system adds a suffix of the form `(k)` to it, where `k` is the smallest positive integer such that the resulting name is still unique. Return a list `ans` of length `n` where `ans[i]` is the actual name the system assigns to the folder created at minute `i`. The output is in the same order as `names`. ### Rules - A name counts as used once it has been assigned to any earlier folder, whether it was requested as is or produced by adding a suffix. Folders are never deleted. - The suffixed name is the requested name followed by `(`, the decimal value of `k` with no leading zeros, and `)`. For example, `"doc"` with `k = 2` becomes `"doc(2)"`. - A requested name that already ends in something like `(1)` is treated as an ordinary string. If `"doc(1)"` is requested while it is taken, the new suffix goes after it, giving `"doc(1)(1)"`. The suffix `k` never exceeds `n`, so every number involved fits in a 32-bit signed integer. ### Example 1 ```text Input: names = ["gta","gta(1)","gta","avalon"] Output: ["gta","gta(1)","gta(2)","avalon"] ``` `"gta(1)"` is requested directly at minute 1. When `"gta"` comes again at minute 2, the suffix `(1)` is already taken, so the smallest free one is `(2)`. `"avalon"` is new. ### Example 2 ```text Input: names = ["doc","doc(1)","doc","doc(1)"] Output: ["doc","doc(1)","doc(2)","doc(1)(1)"] ``` The second `"doc"` skips `(1)` because `"doc(1)"` exists, so it becomes `"doc(2)"`. The second request for `"doc(1)"` collides with the folder from minute 1, so it gets its own suffix and becomes `"doc(1)(1)"`. ### Constraints - `1 <= names.length <= 5 * 10^4` - `1 <= names[i].length <= 20` - `names[i]` consists of lowercase English letters, digits, and/or round brackets.

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

  1. 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.
  2. 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.
  3. 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.

Unique Folder Names with the Smallest Free Suffix

mediumOktaSoftware Engineer

You create n folders in a file system, one per minute. At minute i you create a folder with the requested name names[i].

Two folders cannot have the same name. If you request a name that was used before, the system adds a suffix of the form (k) to it, where k is the smallest positive integer such that the resulting name is still unique.

Return a list ans of length n where ans[i] is the actual name the system assigns to the folder created at minute i. The output is in the same order as names.

Rules

  • A name counts as used once it has been assigned to any earlier folder, whether it was requested as is or produced by adding a suffix. Folders are never deleted.
  • The suffixed name is the requested name followed by (, the decimal value of k with no leading zeros, and ). For example, "doc" with k = 2 becomes "doc(2)".
  • A requested name that already ends in something like (1) is treated as an ordinary string. If "doc(1)" is requested while it is taken, the new suffix goes after it, giving "doc(1)(1)".

The suffix k never exceeds n, so every number involved fits in a 32-bit signed integer.

Example 1

Input: names = ["gta","gta(1)","gta","avalon"]
Output: ["gta","gta(1)","gta(2)","avalon"]

"gta(1)" is requested directly at minute 1. When "gta" comes again at minute 2, the suffix (1) is already taken, so the smallest free one is (2). "avalon" is new.

Example 2

Input: names = ["doc","doc(1)","doc","doc(1)"]
Output: ["doc","doc(1)","doc(2)","doc(1)(1)"]

The second "doc" skips (1) because "doc(1)" exists, so it becomes "doc(2)". The second request for "doc(1)" collides with the folder from minute 1, so it gets its own suffix and becomes "doc(1)(1)".

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

Example 1
Input
(['gta', 'gta(1)', 'gta', 'avalon'],)
Output
['gta', 'gta(1)', 'gta(2)', 'avalon']
Notes
Source example 1 (4 names): the directly requested gta(1) holds suffix 1, so the second gta becomes gta(2).
Example 2
Input
(['doc', 'doc(1)', 'doc', 'doc(1)'],)
Output
['doc', 'doc(1)', 'doc(2)', 'doc(1)(1)']
Notes
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).

Constraints

  • •1 <= names.length <= 5 * 10^4
  • •1 <= names[i].length <= 20
  • •names[i] consists of lowercase English letters, digits, and/or round brackets.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def get_folder_names(names: list[str]) -> list[str]:
Sample call
get_folder_names(['gta', 'gta(1)', 'gta', 'avalon'])
Language
Case 1
Input
(['gta', 'gta(1)', 'gta', 'avalon'],)
Expected
['gta', 'gta(1)', 'gta(2)', 'avalon']
Case 2
Input
(['doc', 'doc(1)', 'doc', 'doc(1)'],)
Expected
['doc', 'doc(1)', 'doc(2)', 'doc(1)(1)']

Browse More Questions

Show the approach

Approach

Keep a hash set used of every name assigned so far and a map next_k from a requested name to the smallest suffix worth trying for it. For each request s: if s is not in used, assign it unchanged and add it to used. Otherwise start at k = next_k.get(s, 1), build s(k), and increase k while that string is in used. Assign the first free s(k), add it to used, and store next_k[s] = k + 1.

Invariant: for every s in next_k, each s(j) with 1 <= j < next_k[s] is already in used. Folders are never deleted, so a taken suffix stays taken, and resuming the scan at next_k[s] finds the same smallest free k that a scan from 1 would find. Generated names go into used as well, which is why a later request for a generated name such as a(1) collides and becomes a(1)(1).

Cost: a failed probe of s(j) lands on a name that is already used. Such a string comes from exactly one pair (s, j), because j is the run of digits between its last ( and its final ), and once next_k[s] moves past j that string is never probed again. So failed probes total at most n, successful probes total at most n, and each probe builds and hashes a string of at most 27 characters (20 for the name, up to 5 digits and 2 brackets).

Edge cases: a single name is returned unchanged; requested strings such as a(0), n(01), () or x(2) are ordinary names, so x(2) requested first does not make x used and n(01) never blocks n(1); the suffix starts at 1, never 0; suffixes of two or more digits are written in plain decimal.

Time complexity:
O(n * L), where L is at most 27, the longest assigned name
Space complexity:
O(n * L)