Skip to main content

5. Remove Element

easyAsked at Snowflake

Remove all instances of a target value from an array in-place. Snowflake uses this to test whether you can write a tight scan loop — the same shape as a predicate pushdown filter over a column chunk.

By Sam K., Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Snowflake loops.

  • Glassdoor (2025-Q4)Snowflake SDE-I phone screen warm-up.
  • Blind (2025-12)Reported at Snowflake intern screens.

Problem

Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The order of the elements may be changed. Then return the number of elements in nums which are 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

Build a new array with everything except val.

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

Tradeoff: Violates the in-place requirement. Mention to acknowledge but reject.

2. Two-pointer write-index (optimal)

Walk the array. Maintain a write index that only advances when the current element is not val.

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

Tradeoff: Single pass, O(1) extra space. Identical loop to a vectorized predicate filter.

Snowflake-specific tips

Snowflake interviewers want the read/write pointer pattern stated explicitly. Bonus signal: discuss the vectorized version — process in batches of 64 with a bitmask, the way Snowflake's execution engine actually evaluates predicates on column chunks.

Common mistakes

  • Using two pointers from opposite ends and swapping — works but changes order more than necessary, and complicates reasoning.
  • Returning the array instead of the count.
  • Forgetting to mutate nums (just incrementing a counter doesn't change the array).

Follow-up questions

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

  • What if you must preserve the original order of remaining elements?
  • What if val could be a list of values?
  • Vectorize this — process 64 elements at a time with a bitmask.

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 you swap from the end instead?

If val is rare, swapping from the end minimizes writes. If val is common, the write-pointer pattern is better. Snowflake interviewers like the trade-off discussion.

How does this connect to predicate pushdown?

When Snowflake filters WHERE col != val, the engine runs exactly this loop over each column chunk, often vectorized with SIMD. The shape of the algorithm is identical.

Companies that also ask Remove Element