Code RoomExpression language lexer
MediumPrep Room Coding #1433

Expression language lexer

CodingAlgorithms & data structuresMid–Senior~25 min

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.

0:00 of about 25 min
InputExpectedGot
["3 + x"][["NUM","3"],["OP","+"],["IDENT","x"]]not run yetsample