Auto-Rename Colliding Uploads in a Shared Folder
Last updated: November 29, 2026
Quick Overview
Given the names already in a folder and a batch of file names being uploaded in order, return the final name for each upload, appending " (k)" before the extension with the smallest free k whenever a name collides case-insensitively.
Box
November 29, 20260
0
0 solved
Given the names already in a folder and a batch of file names being uploaded in order, return the final name for each upload, appending " (k)" before the extension with the smallest free k whenever a name collides case-insensitively.
Box folders are shared by many collaborators and Box Drive uploads whole directories at once, so name collisions are routine and the rename rule has to be deterministic. A phone screen uses this to check string handling, hashing, and whether you notice case-insensitivity and dotfiles without prompting.
What the Interviewer Expects
- Treats names case-insensitively, as most file systems synced through Box Drive do
- Splits stem and extension on the last dot, but keeps dotfiles like .env whole
- Picks the smallest unused k, including skipping names that already look like "x (1).txt"
- Avoids rescanning from k=1 every time by caching the next index per stem
- States O(total name length) expected time
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you make this safe when two uploads to the same folder race on different servers?
- Should "a.tar.gz" become "a (1).tar.gz" instead? How would you support multi-part extensions?
- What changes if a user deletes "report (1).pdf" and later uploads report.pdf again?
- How would you surface the rename to the user so they are not surprised?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: existing, the names in the folder, and uploads, names arriving in order. Output: the stored name for each upload. Names compare case-insensitively ("Report.pdf" blocks "report.pdf") but we ...
High-Level Design
Keep a set taken of lowercased names. For each upload, if it is free, take it. Otherwise split on the last dot (only when the dot is not at index 0), then probe k upward. To avoid quadratic behaviou...