Description
Solutions
Goto Largest Bucket
🤘 INTERN

To view the original problem statement, pls check out the source image below 🦁

Imagine you're navigating through a file system that has several storage compartments, each with a unique name—let's call them "buckets." You can move between these buckets and create files within them. Your task is to follow a series of commands and, at the end of the day, determine which bucket ended up holding the most files.

Here is how the story unfolds:

  • Move to a bucket: Whenever you're told to "goto" a certain bucket, you immediately jump to that bucket. You can assume that the bucket always exists, so there's no need to worry about getting lost..
  • Create a file: If you're in a bucket and receive a command to "create" a file, you simply add a new file with the specified name to that bucket. If a file with that name already exists in the current bucket, you skip the creation—no duplicates allowed.
  • Your mission is to process all these commands one by one. At the end of all the instructions, your goal is to figure out which bucket contains the most files. You’re guaranteed that there won’t be any ties, so there will always be a clear winner.

    Here are some things to keep in mind:

  • The very first command you get will always tell you to move to a specific bucket, so you’ll never start out lost.
  • At least one of the commands will be about creating a file, so you’ll have some files to work with.
  • Don’t worry too much about finding the most optimal way to do this; as long as your approach isn’t too slow, it will work just fine.
  • Your final task is to identify the bucket that ended up with the most files after all the commands have been processed, and that’s the bucket you’ll declare as the winner!

    Example 1:

    Input:  commands = ["goto bucketA", "create fileA", "create fileB", "create fileA", "goto bucketB", "goto bucketC", "create fileA", "create fileB", "create fileC"]
    Output: "bucketC"
    Explanation:
    This explanation is just an educated guess. If you find anything wrong, pls dont hesitate to lmk! Many thanks in advance! 🧡 For the given commands, the number of files in each bucket is as follows:
  • bucketA: 2 files (fileA and fileB)
  • bucketB: 0 files
  • bucketC: 3 files (fileA, fileB, and fileC)
  • Since bucketC has the largest number of files, the output is "bucketC".
    Constraints:
      🦅🙉
    Thumbnail 0
    Thumbnail 1
    Testcase

    Result
    Case 1

    input:

    output: