Skip to main content

5. Remove Element

easyAsked at Databricks

Remove all occurrences of a value from an array in-place. Databricks uses this as the in-place-filter primitive that maps onto Spark's filter operator on a partition.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Databricks loops.

  • Glassdoor (2025-08)Databricks runtime engineer phone screen.
  • Blind (2026-Q1)Often paired with 'how does Spark predicate pushdown skip rows in Parquet?'

Problem

Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The relative order of the elements may be changed. Return k, the number of elements not equal to val.

Constraints

  • 0 <= nums.length <= 100
  • 0 <= nums[i] <= 50
  • 0 <= val <= 100

Examples

Example 1

Input
nums = [3,2,2,3], val = 3
Output
2, nums = [2,2,_,_]

Example 2

Input
nums = [0,1,2,2,3,0,4,2], val = 2
Output
5, nums = [0,1,4,0,3,_,_,_]

Approaches

1. Filter into new array

Copy non-val elements into a new array.

Time
O(n)
Space
O(n)
function removeElement(nums, val) {
  const out = nums.filter(x => x !== val);
  for (let i = 0; i < out.length; i++) nums[i] = out[i];
  return out.length;
}

Tradeoff: O(n) extra space defeats the in-place requirement.

2. Two pointers (write head advances on keep)

Walk fast; when nums[fast] != val, write to nums[slow] and advance slow.

Time
O(n)
Space
O(1)
function removeElement(nums, val) {
  let slow = 0;
  for (let fast = 0; fast < nums.length; fast++) {
    if (nums[fast] !== val) {
      nums[slow] = nums[fast];
      slow++;
    }
  }
  return slow;
}

Tradeoff: O(1) space. Same read/write-head pattern as Remove Duplicates — recognize the family.

Databricks-specific tips

Databricks grades whether you can articulate this as 'filter in-place' and connect it to how a Spark filter operator runs on a partition with zero allocation. The bonus signal is mentioning that if removal is rare (sparse filter), you can swap with the end instead of compacting — the same trick Spark uses for highly selective predicates with vectorized columnar storage.

Common mistakes

  • Comparing != val twice (once to decide, once to write) — single comparison is enough.
  • Trying to return the original length minus a counter — the cleaner formulation tracks 'slow' directly.
  • Splicing in JavaScript with nums.splice() — that's O(n) per call and turns this into O(n^2).

Follow-up questions

An interviewer at Databricks may pivot to one of these next:

  • Order doesn't matter — swap-with-end variant for O(swaps) instead of O(n).
  • Remove multiple distinct values in one pass.
  • Spark filter operator: how does Catalyst push this predicate down into Parquet row-group skipping?

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

When would I swap-with-end instead of compact?

When val is rare. Compact writes every kept element (n writes); swap-with-end only writes the matches (k writes). For k << n, swap-with-end wins.

Does the order have to be preserved?

The problem says order MAY be changed. Compact preserves it for free; swap-with-end doesn't. Either is accepted.

Practice these live with InterviewChamp.AI

Drill Remove Element and other Databricks interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →