Move Deleted Photos to the End of an Album In Place

Last updated: November 29, 2026

Quick Overview

An album is an array of photo IDs where 0 marks a deleted photo. Rearrange the array in place so all non-zero IDs keep their relative order at the front and all zeros move to the back.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Easy

0

0

0 solved


An album is an array of photo IDs where 0 marks a deleted photo. Rearrange the array in place so all non-zero IDs keep their relative order at the front and all zeros move to the back.

Framed around Google Photos compaction after a user deletes items, this is a common online-assessment or warm-up question. The interviewer wants a stable, in-place read/write pointer pass and an explanation of why it uses the minimum number of writes.

What the Interviewer Expects
  • Uses a read pointer and a write pointer
  • Keeps the relative order of surviving photos
  • Works in place with O(1) extra memory
  • Explains the swap version versus the overwrite-then-fill version
  • Handles arrays with no zeros or only zeros
Key Topics to Cover
Two Pointers
In-Place Arrays
Stable Partition
Compaction
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 total number of writes if writes are expensive (for example, on flash storage)?
  • What if deleted entries are marked by a predicate instead of 0?
  • How would you compact a list stored across many disk pages?
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: a list ids of integers; 0 means deleted.
  • Modify the list in place; return it for convenience.
  • Non-zero IDs must keep their original relative order.
  • O(1) extra space and a single pas...
High-Level Design

Keep a write index that marks where the next surviving photo should go. Scan with a read index. Each time ids[read] is non-zero, swap it with ids[write] and advance write. Everything before ...


Submit Your Answer
Markdown supported