FastPrepShell Command Counts with History References
Problems/Shell Command Counts with History References

Shell Command Counts with History References

TikTok logoTikTok● EasyFULLTIMEOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report specifies the three commands, the one-based references, and the output order. We added numeric limits and a rule that every reference points backward to a valid command. Our version is about 95% the same as the reported question.

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
All TikTok interview questionsSee TikTok hiring insights
Saved
class Solution {
    public int[] countShellCommands(String[] history) {
        // Write your code here.
    }

}
Checking account…