Code Room
CodingEasycod-g1245
Subject HashingLevel Entry–Mid~10 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

You are compacting a list of integers with a soft limit: each distinct value may appear at most twice in the output. Scan the list from left to right and keep an element only if its value has been kept fewer than two times so far; drop it otherwise. The relative order of kept elements must match the original list. For example, [1, 1, 1, 2, 2, 3] becomes [1, 1, 2, 2, 3], and [9, 8, 9, 8, 9, 7] becomes [9, 8, 9, 8, 7]. The input is not sorted.

Implement
cap_two_per_value(nums: list[int]) → list[int]
Examples
in[[1,1,1,2,2,3]]out[1,1,2,2,3]
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.