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
November 29, 20260
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
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- 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 ProblemsSample 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...