Final Resting State of Boxes Tipped Along a Packing Line

Last updated: November 29, 2026

Quick Overview

A row of upright boxes is given as a string where 'L' means pushed left, 'R' means pushed right and '.' is untouched. Each second a falling box tips its upright neighbor in the same direction; a box pushed from both sides at once stays upright. Return the final string.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


A row of upright boxes is given as a string where 'L' means pushed left, 'R' means pushed right and '.' is untouched. Each second a falling box tips its upright neighbor in the same direction; a box pushed from both sides at once stays upright. Return the final string.

An Amazon onsite simulation question. A second-by-second simulation works but the interviewer wants the linear scan over segments between forces, or the two-pass force computation, and clear handling of the balanced middle.

What the Interviewer Expects
  • Recognizes that only the nearest forces on each side matter
  • Handles R...L by filling inward and keeping the middle upright for odd gaps
  • Handles L...L, R...R, L...R correctly
  • Runs in O(n)
  • Pads with virtual L at the start and R at the end
Key Topics to Cover
simulation
two pointers
string processing
force propagation
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
  • Return the time when each box falls.
  • What if a push travels only k positions?
  • What if boxes have weights that resist tipping?
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: string over {'L', 'R', '.'}, length up to 10^5. Output: final string after all motion stops....

High-Level Design

Add a sentinel 'L' at the left end and 'R' at the right end. Walk over consecutive pairs of non-dot positions i < j with the dots between them. If both are the same letter, fill the gap with that lett...


Submit Your Answer
Markdown supported