Compact Nonzero Reaction Counts to the Front In Place

Last updated: November 29, 2026

Quick Overview

Given an array of reaction counts for a post, move every zero to the end while keeping the relative order of the nonzero counts. Do it in place with O(1) extra space.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Given an array of reaction counts for a post, move every zero to the end while keeping the relative order of the nonzero counts. Do it in place with O(1) extra space.

A warm-up in the style of Meta's phone screen, where the first problem is short and the interviewer wants clean code in under 15 minutes so a second problem fits. They check that you reach the two-pointer, in-place solution directly and minimize writes.

What the Interviewer Expects
  • States the in-place and order-preserving constraints before coding
  • Uses a write pointer that trails a read pointer
  • Avoids an extra array and avoids O(n^2) shifting
  • Mentions minimizing writes when most values are already nonzero
  • Tests with all zeros, no zeros, and an empty array
Key Topics to Cover
Two pointers
In-place array mutation
Stable partition
Arrays
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you minimize the number of array writes?
  • What if zeros should go to the front instead?
  • How would you remove all values equal to some target and return the new length?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input is a list of non-negative integers. Mutate it in place, keep nonzero order stable, and return nothing. Expected O(n) time and O(1) extra space. Confirm that negative values are not possible, tho...

High-Level Design

Keep a write index pointing at the next slot where a nonzero value belongs. Scan with a read index. Each time a nonzero value appears, swap it into write and advance write. Because write nev...


Submit Your Answer
Markdown supported