Parse Command-Line Tokens
Updated
Problem statement
Parse an already-tokenized command line tokens and return occurrence-ordered rows.
- A long option begins with
--; a short option begins with one-. --name=valueand-n=valueproduce[name, value].- An option without
=consumes the next token as its value only when that token does not begin with-; otherwise its value istrue. - 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
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