← Questions

Bucket with the most distinct files after goto and create commands

0
DatabricksCodingLast asked Aug 2024

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 a goto, there is at least one create, 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 create with 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> and goto .. 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

What it tests

hash maps · simulation

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