Code RoomTerm frequency
EasyPrep Room Coding #1552

Term frequency

CodingML systemsEntry–Mid~18 min

Compute term frequencies for a tiny document. Given a document string, lowercase it and split on whitespace into tokens, then return a list of [term, frequency] pairs where frequency is the count of that term divided by the total number of tokens. Include each distinct term once, ordered alphabetically by term. If the document is empty or all-whitespace (zero tokens), return an empty list. Round each frequency to 4 decimal places.

Implement
term_frequency(doc: str) → list[list]
Examples
in["the cat the dog"]out[["cat",0.25],["dog",0.25],["the",0.5]]
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 18 min
InputExpectedGot
["the cat the dog"][["cat",0.25],["dog",0.25],["the",0.5]]not run yetsample