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

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.

Okta logo
Okta
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

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 ith folder when you create it.

Function Signature

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

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)".

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.

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)".