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

Question

Write a lexer for a tiny expression language. Given a source string, return a list of `[type, lexeme]` tokens. Token types are: `NUM` (an integer or decimal like `12` or `2.5` or `.5`), `IDENT` (a name starting with a letter or underscore, then letters/digits/underscores), `OP` (one of `+ - * /`), `PAREN` (`(` or `)`), and `UNKNOWN` (any single other non-space character). Spaces separate tokens but are not emitted. Return tokens in source order.

Implement
tokenize(expr: str) → list[list]
Examples
in["3 + x"]out[["NUM","3"],["OP","+"],["IDENT","x"]]
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.