Code RoomExpression lexer
MediumPrep Room Coding #1022

Expression lexer

CodingAlgorithms & data structuresMid–Senior~30 min

Write a lexer for a small expression language. Given a source string, produce a list of [type, value] tokens. Token types: 'NUM' for integer or decimal numbers (digits with at most one optional internal dot, e.g. '12', '3.14'), 'ID' for identifiers (a letter or underscore followed by letters/digits/underscores), 'OP' for any of the single chars + - * / ( ) = , and 'WS' is skipped (spaces and tabs are ignored, never emitted). The value of a NUM/ID token is the matched substring; the value of an OP is the single char. Unknown characters never occur. Return the token list in order.

Implement
tokenize_expr(src: str) → list[list]
Examples
in["x = 3.14 + y2"]out[["ID","x"],["OP","="],["NUM","3.14"],["OP","+"],["ID","y2"]]
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 30 min
InputExpectedGot
["x = 3.14 + y2"][["ID","x"],["OP","="],["NUM","3.14"],["OP","+"],["ID","y2"]]not run yetsample