Code Room
CodingMediumcod-g1244
Subject HashingLevel Entry–Mid~14 minCommon in Databases & SQL · Algorithms & data structures interviewsIndustries Software development

Question

Given a list of integers that may contain repeats, produce a new list that keeps only the last occurrence of each distinct value, with the survivors appearing in the same relative order they hold in the original list. For example, [1, 2, 1, 3, 2] keeps the 1 at index 2, the 3 at index 3, and the 2 at index 4, so the answer is [1, 3, 2]. Note this is not the same as keeping first occurrences and cannot be solved by a single forward filter without preparation.

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