FastPrepParse Command-Line Tokens
Problems/Parse Command-Line Tokens

Parse Command-Line Tokens

Samsara logoSamsara● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Samsara onsite interview. The report asks you to parse command-line arguments into a usable structure. It does not give a concrete grammar or the shape of the result. Here your function gets a finite list of tokens. Our version is about 78% the same as the reported question.

Parse an already-tokenized command line tokens and return occurrence-ordered rows.

  • A long option begins with --; a short option begins with one -.
  • --name=value and -n=value produce [name, value].
  • An option without = consumes the next token as its value only when that token does not begin with -; otherwise its value is true.
  • The token -- ends option parsing and is not returned. Every later token is positional.
  • A positional token produces ["", token].

Preserve option and positional occurrences exactly; do not merge repeated names.

Function

parseCommandLine(tokens: String[]) → String[][]

Examples

Example 1

tokens = ["--host=api","-p","8080","file.txt"]return = [["host","api"],["p","8080"],["","file.txt"]]

The first option uses equals, the short option consumes its following value, and the filename is positional.

Example 2

tokens = ["--verbose","--","-x","tail"]return = [["verbose","true"],["","-x"],["","tail"]]

The flag has value true; after the terminator, option-looking tokens are positional.

Constraints

  • 0 <= tokens.length <= 100000.
  • Every token is nonempty.
  • Option names before = are nonempty.

More Samsara interview questions

  • Render a Source-Backed Markdown SubsetPHONE SCREEN · Seen Jul 2024

Similar questions at other companies

  • Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
  • Validate Typed CSV RecordsStripe · PHONE SCREENStripe interview questions
  • Machine Message RouterOpenAI · PHONE SCREENOpenAI interview questions
  • Correct Apartment Bedroom CountsRamp · OARamp interview questions
  • Reversible Run-Length EncodingZillow · PHONE SCREENZillow interview questions
  • Format CSV Rows as an ASCII TableAmazon · PHONE SCREEN
  • Rank Word-Translation Hints from Sentence TranslationsDuolingo · PHONE SCREEN · ONSITE INTERVIEWDuolingo interview questions
  • Response Time Stats by URL PatternGusto · ONSITE INTERVIEWGusto interview questions
  • Digit-Coded Card ValidationCapital One · ONSITE INTERVIEWCapital One interview questions
  • Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
All Samsara interview questionsSee Samsara hiring insights
Saved
class Solution {
    public String[][] parseCommandLine(String[] tokens) {
        // Write your code here.
    }
}
Checking account…