← Questions
Fewest deletions so a list's first k items avoid another list
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Google's first round (coding), last asked Feb 2026. It tests arrays, hash sets and greedy.
You get two lists, a and b, and an integer k. You may delete elements from b; the elements you keep stay in their original order. Make sure none of the first k elements of what remains appears in a. Return the fewest deletions needed.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
min_deletions(a: list[int], b: list[int], k: int) -> int— ifbhas fewer thankelements that are not ina, every element ofbthat is inamust go, and the result has fewer thankitems.
Example
a = [2, 4], b = [2, 1, 4, 4, 3, 5], k = 2 -> 3 # delete 2, 4, 4; the result starts [1, 3]
a = [1], b = [1, 1, 2], k = 3 -> 2 # only one usable element exists
a = [9], b = [1, 2, 3], k = 2 -> 0
Constraints
- Up to 10^5 elements in each list.
- Values are integers and may repeat.
Clarifying questions
- Can I delete elements from
a, or only fromb? - Must the order of
bbe kept? - What if
bruns out of usable elements beforek? - Do you want the count, the resulting list, or both?
What a strong answer covers
- A hash set of
afor O(1) membership checks. - One greedy scan of
b: count usable elements kept and the elements ofamet before thek-th usable one; nothing after that position matters. - Why the greedy scan is optimal: every element of
abefore thek-th kept element must be deleted, and deleting usable elements never helps. - O(len(a) + len(b)) time.
- Tests:
k = 0,bshorter thank, no overlap at all.
Follow-ups
- Return the resulting list, not only the count.
- Deleting from
bhas a cost per element: minimize total cost. - The lists are streams too large for memory.
Where it's asked
- Google · First round: coding · Last asked Feb 202645 min · Coding in a Google Doc, no syntax highlighting or autocomplete · No AI
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
