← Questions

Fewest deletions so a list's first k items avoid another list

0
GoogleCodingLast asked Feb 2026

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 — if b has fewer than k elements that are not in a, every element of b that is in a must go, and the result has fewer than k items.

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 from b?
  • Must the order of b be kept?
  • What if b runs out of usable elements before k?
  • Do you want the count, the resulting list, or both?

What a strong answer covers

  • A hash set of a for O(1) membership checks.
  • One greedy scan of b: count usable elements kept and the elements of a met before the k-th usable one; nothing after that position matters.
  • Why the greedy scan is optimal: every element of a before the k-th kept element must be deleted, and deleting usable elements never helps.
  • O(len(a) + len(b)) time.
  • Tests: k = 0, b shorter than k, no overlap at all.

Follow-ups

  • Return the resulting list, not only the count.
  • Deleting from b has 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

arrays · hash sets · greedy

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