← Questions
Bucket with the most distinct files after goto and create commands
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Databricks's online assessment, last asked Aug 2024. It tests hash maps and simulation.
Process a list of commands in a small virtual file system. goto <bucket> makes that bucket the current one. create <file> adds the file to the current bucket; creating a name that already exists in that bucket does nothing. After all commands, return the bucket that holds the most distinct files.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
largest_bucket(commands: list[str]) -> str— the first command is agoto, there is at least onecreate, and the answer is unique.
Example
largest_bucket(["goto bucketA", "create fileA", "create fileB", "create fileA",
"goto bucketB", "goto bucketC", "create fileA", "create fileB", "create fileC"])
-> "bucketC" # A has 2 distinct files, B has 0, C has 3
Constraints
- Bucket and file names have no spaces.
Clarifying questions
- Is the same file name in two different buckets two files? (Yes.)
- What if two buckets tie for the most files?
- Can the first command be a
createwith no current bucket?
What a strong answer covers
- A dict from bucket name to a set of file names, and the current bucket as a variable.
- O(total commands) time; set membership handles duplicates.
- Choosing the maximum at the end, and a clear tie rule if ties become possible.
- Tests: repeated creates, buckets visited but empty, switching back to an earlier bucket.
Follow-ups
- Add
delete <file>andgoto ..with nested folders. - Answer "largest bucket so far" after every command in O(1) or O(log n).
- Count total size when each file has a size.
Where it's asked
- Databricks · Online assessment · Last asked Aug 2024Timed online coding test
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
