Command Frequency Counter
Updated
Problem statement
You are given an array of commands. There are only three possible commands: cmd1, cmd2, and cmd3. Return an array of size 3 that contains the frequency of each command in the input array, in the order [frequency of cmd1, frequency of cmd2, frequency of cmd3].
An element can also have the form !k, where k is a position in the array. It stands for the same command as the element at position k, and it counts toward that command. Positions start from 1 (not 0).
A reference always points to an earlier position. If it points to another reference, follow that one too until you reach a real command. For example, in ["cmd2", "!1", "!2"] both references mean cmd2.
Complete the function commandFrequencyCounter in the editor.
commandFrequencyCounter has the following parameter:
String[] commands: an array of commands
The function returns an int[] of size 3: the number of times cmd1, cmd2 and cmd3 appear, with every reference counted as the command it stands for.
Function
commandFrequencyCounter(commands: String[]) → int[]Examples
Example 1
commands = ["cmd1", "cmd2", "cmd3", "!1", "!2", "cmd3", "cmd1"]return = [3, 2, 2]At position 4, !1 stands for the command at position 1 (cmd1). At position 5, !2 stands for the command at position 2 (cmd2). So cmd1 appears 3 times (positions 1, 4, 7), cmd2 2 times (positions 2, 5) and cmd3 2 times (positions 3, 6).
Constraints
1 ≤ commands.length ≤ 105- Each
commands[i]is"cmd1","cmd2","cmd3", or"!k"for an integerkwritten without leading zeros. - In a reference
"!k"at positionp(1-based),1 ≤ k < p.
More Uber interview questions
- Random Bingo Card GeneratorONSITE INTERVIEW · Seen Dec 2026
- Travelers per Consecutive City TripletONSITE INTERVIEW · Seen Dec 2026
- Worst-Performing Ads by CTR in a Campaign TreePHONE SCREEN · Seen Nov 2026
- Earliest Time All Users Are ConnectedPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Max Points on a LinePHONE SCREEN · Seen Oct 2026
- Online Meeting Scheduler with Fixed RoomsONSITE INTERVIEW · Seen Oct 2026
- Convex Function MinimizationPHONE SCREEN · Seen Oct 2026
- Meeting Rooms IIPHONE SCREEN · Seen Oct 2026
- Merge Meeting IntervalsPHONE SCREEN · Seen Oct 2026
- Minimum Meeting Rooms with AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Macaron Order with Set DiscountsShopify · OAShopify interview questions
- Basic Item DiscountsPalantir · OAPalantir interview questions
- Calculate Portfolio Rebalancing DeltasAkuna Capital · OAAkuna Capital interview questions
- Filter Transaction Logs and Calculate Reliability MetricsStripe · OAStripe interview questions
- Get Total RequestsAmazon · OA
- Balloon Color PairsInstacart · OAInstacart interview questions
- Cargo Port Ship StopsNutanix · OANutanix interview questions
- Bank Accounts with Balance HistoryMeta · OAMeta interview questions
- Parcel Attribute Store with TTLAirbnb · OAAirbnb interview questions
- Process Banking OperationsVercel · OA