Code Room
CodingMediumcod-g670
Subject TokenizationLevel Mid–Senior~30 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

Write a tokenizer for a tiny arithmetic language. Given a source string, produce a list of [type, value] token pairs. Token types: 'NUM' for integer or decimal numbers (e.g. 12, 3.5), 'ID' for identifiers (letter or underscore followed by letters/digits/underscores), 'OP' for any single character in +-*/(), and 'WS' is skipped entirely. Any other character raises a ValueError. Numbers and identifiers are greedy (longest match). Return the list of [type, value] pairs in order.

Implement
tokenize(src: str) → list[list[str]]
Examples
in["x1 + 3.5 * (foo)"]out[["ID","x1"],["OP","+"],["NUM","3.5"],["OP","*"],["OP","("],["ID","foo"],["OP",")"]]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.