Allocate Unique File Names in an In-Memory Vault

Read the full interview experience this question came from →

Quick Overview

Allocate unique vault file names with the smallest available numeric suffix, handling preexisting suffixes, repeated requests, and case sensitivity.

Allocate Unique File Names in an In-Memory Vault

Company: Harvey

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

An in-memory file vault must assign a unique name whenever a file is created. If the requested name already exists, append a parenthesized positive integer to obtain an unused name. Implement `allocate_names(requests: string[]) -> string[]` and return the assigned name for each request in order. ### Constraints & Assumptions - All requests target the same namespace, which starts empty. No file is deleted or renamed during the sequence. - If requested name `s` is unused, assign `s` unchanged. - Otherwise choose the smallest positive integer `x` for which `s + "(" + decimal(x) + ")"` is unused, then reserve and return that name. - Names are case-sensitive, nonempty ASCII strings of at most 100 characters. There are at most 10,000 requests. - Parentheses already present in a requested name are literal text. For example, a collision on `a(1)` tries `a(1)(1)`; do not reinterpret it as a collision on `a`. - The smallest available positive suffix and flat namespace are explicit practice choices for the reported duplicate-name behavior. ### Examples ```text requests = ["report", "report", "report", "report(1)"] result = ["report", "report(1)", "report(2)", "report(1)(1)"] ``` ```text requests = ["a(1)", "a", "a", "A"] result = ["a(1)", "a", "a(2)", "A"] ``` ```hint Remember where each repeated base can resume Once a suffix candidate is known to be occupied and names are never removed, trying the same candidate again for that base cannot succeed. ```

Overview: Allocate unique vault file names with the smallest available numeric suffix, handling preexisting suffixes, repeated requests, and case sensitivity.

Read the full Harvey Software Engineer interview experience this question came from

|Home/Coding & Algorithms/Harvey
Harvey logo
Harvey
Sep 1, 2026
mediumSoftware EngineerOnsiteCoding & Algorithms
0
0

An in-memory file vault must assign a unique name whenever a file is created. If the requested name already exists, append a parenthesized positive integer to obtain an unused name.

Implement allocate_names(requests: string[]) -> string[] and return the assigned name for each request in order.

Constraints & Assumptions

  • All requests target the same namespace, which starts empty. No file is deleted or renamed during the sequence.
  • If requested name s is unused, assign s unchanged.
  • Otherwise choose the smallest positive integer x for which s + "(" + decimal(x) + ")" is unused, then reserve and return that name.
  • Names are case-sensitive, nonempty ASCII strings of at most 100 characters. There are at most 10,000 requests.
  • Parentheses already present in a requested name are literal text. For example, a collision on a(1) tries a(1)(1) ; do not reinterpret it as a collision on a .
  • The smallest available positive suffix and flat namespace are explicit practice choices for the reported duplicate-name behavior.

Examples

requests = ["report", "report", "report", "report(1)"]
result = ["report", "report(1)", "report(2)", "report(1)(1)"]
requests = ["a(1)", "a", "a", "A"]
result = ["a(1)", "a", "a(2)", "A"]

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...