Shell Command Counts with History References
Updated
Problem statement
Process the entries of history in order. Each entry is a base command cp, ls, or mv, or a history reference !index.
A reference executes the same base command that was executed by the entry at the one-based position index. That earlier entry may itself be a reference. Each history entry executes exactly one base command.
Return the total execution counts in the order [cp, ls, mv], including direct commands and references.
For this exercise, assume every reference points to an earlier entry and uses a positive decimal index without leading zeros. All input entries are valid.
Function
countShellCommands(history: String[]) → int[]Examples
Example 1
history = ["cp","ls","!1","mv","!3"]return = [3,1,1]The executed commands are cp, ls, cp, mv, and cp. The last entry references an earlier reference.
Example 2
history = ["mv","!1","!2","ls"]return = [0,1,3]The first three entries all execute mv; the fourth executes ls.
Constraints
- For this exercise, assume
1 <= history.length <= 10^5. - Each entry is
cp,ls,mv, or!index. - For a reference at one-based position
i,1 <= index < i.
More TikTok Online Assessment questions
- Center of the Largest Perfect X-ShapeOA · Seen Nov 2026
- Count Subarrays Matching a Comparison PatternOA · Seen Nov 2026
- Count Suffix Word PairsOA · Seen Nov 2026
- Minimum Cost of Every K-Capable Model SetOA · Seen Nov 2026
- Count Subarrays with at Least K Equal-Fruit PairsOA · Seen Oct 2026
- Product Minus Sum of DigitsOA · Seen Aug 2026
- Count 2x2 Submatrices by Black CellsOA · Seen Aug 2026
- Find All Local PeaksOA · Seen Aug 2026
- Count Case-Insensitive TripletsOA · Seen Aug 2026
- Optimize TikTok Reels ViewingOA · Seen Aug 2026
Similar questions at other companies
- Macaron Order with Set DiscountsShopify · OAShopify interview questions
- Count Card Duel RoundsAirbnb · OAAirbnb interview questions
- 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
- Count Dropped RequestsJP Morgan Chase · OAJP Morgan Chase interview questions
- Recursively Expanded Shell Command CountsHudson River Trading · OAHudson River Trading interview questions
- Diagonal Bounce TraversalAutodesk · OAAutodesk interview questions
TikTok interview experiences
- TikTok AI Infra Intern LLM Round 2: 1 Experience
- TikTok AI Infra Intern DeepSpeed Round: 1 Experience
- TikTok AI Infra Intern Kernel Round: 1 Experience
- TikTok AI Infra Intern Interviews, 3 Rounds: 1 Experience
- TikTok AI Infra Intern Interview Timeline: 1 Experience
- TikTok AI Infra Intern Onsite Round 2: 1 Experience