Recursively Expanded Shell Command Counts
Updated
Problem statement
You are given a command history. Every entry is one of the base commands cp, ls, and mv, or a history reference of the form !index.
Each index is one-based, refers to an earlier entry, and executes the command represented by that entry. A reference may point to another reference, so resolve references recursively.
Return the total execution counts in the order [cp, ls, mv], counting both direct executions and executions reached through references.
Function
countShellCommands(history: String[]) → int[]Examples
Example 1
history = ["ls","cp","!1","!3","mv"]return = [1,3,1]Entries 1, 3, and 4 execute ls. The direct cp and mv entries execute once each.
Example 2
history = ["cp","!1","!2"]return = [3,0,0]Both references ultimately resolve to cp, so all three entries execute that command.
Example 3
history = ["mv","ls","cp","!2","!1","!4"]return = [1,3,2]The final reference points to entry 4, which resolves to ls. The totals are one cp, three ls executions, and two mv executions.
Constraints
1 <= history.length <= 10^5.- Every entry is
cp,ls,mv, or!index. - Every referenced
indexis between1and the current entry's one-based position minus1.
More Hudson River Trading Online Assessment questions
- Count Palindrome Addition ChoicesOA · Seen Nov 2026
- Count Multiples of Three with Two SevensOA · Seen Oct 2026
- Rotate Matrix Regions Around the DiagonalsOA · Seen Oct 2026
- Maximum Diamond SumOA · Seen Oct 2026
- Smallest Most-Lit Street PositionOA · Seen Oct 2026
- Rearrange String by Alternating EndsOA · Seen Sep 2026
- Most Items Within Budget from a Start IndexOA · Seen Sep 2026
- Count Fancy NumbersOA · Seen Sep 2026
- Count Sawtooth SubarraysOA · Seen Aug 2026
- Backtick Identifier ConverterOA · Seen Jun 2026
Similar questions at other companies
- Catch Fish with Reusable BaitsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Book Reading EveningsGoogle · OAGoogle interview questions
- Basic Item DiscountsPalantir · OAPalantir interview questions
- Wind-Blown Leaves Remaining in a GridAbridge · OAAbridge interview questions
- Lift Total TravelInfosys · OAInfosys interview questions
- Shell Command Counts with History ReferencesTikTok · OATikTok interview questions
- Count Dropped RequestsJP Morgan Chase · OAJP Morgan Chase interview questions
- Diagonal Bounce TraversalAutodesk · OAAutodesk interview questions
- First Value Below Both NeighborsCapital One · OACapital One interview questions
- REST API: Highest International StudentsRippling · OARippling interview questions