FastPrepBranch with the Most Files

Branch with the Most Files

Capital One logoCapital One● EasyFULLTIMEOA
Learn

Problem statement

Process the version-control log entries in logs in order. Initially no branch is selected.

  • switch branch selects the named branch, creating it with a count of zero if necessary.
  • push filename adds one pushed-file event to the selected branch. Repeated filenames count as separate pushes.

Return the name of the branch with the greatest number of push events. If several branches tie, return the one that received its first push earliest. Return the empty string "" when no pushes occurred; this represents the absence of a branch.

For this exercise, assume every push follows a switch, each branch name and filename is a nonempty token without spaces, repeated filenames count separately, and ties use the earliest first push.

Function

branchWithMostFiles(logs: String[]) → String

Examples

Example 1

logs = ["switch main","push a.py","push b.py","switch dev","push c.py","switch main","push d.py"]return = "main"

The main branch receives three pushes and dev receives one, so the result is main.

Constraints

  • For this exercise, assume 0 <= logs.length <= 10^5.
  • Every entry is exactly switch branch or push filename with one separating space.
  • For this exercise, assume branch names and filenames have lengths from 1 through 100.
  • Every push occurs after at least one switch.

More Capital One problems

See Capital One hiring insights
public String branchWithMostFiles(String[] logs) {
    // Write your code here.
}
logs["switch main","push a.py","push b.py","switch dev","push c.py","switch main","push d.py"]
expected"main"
Checking account…