Reported December 2024
Duolingosimulation

Bouncing Square Reaches a Screen Corner

Reported by candidates from Duolingo's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Duolingo OA. Under 2s to a working solution.
Founder's read

Duolingo reported this one in December 2024, and it looks friendlier than it is. A 2x2 square bounces diagonally around a screen and you return the steps until it covers a corner. The edge case that breaks a naive solution is the loop: some starts never hit a corner, and a plain simulation spins forever. If you've got an OA invite, expect to handle -1 cleanly. StealthCoder sits as a quiet safety net during the live assessment if the cycle logic slips away, but the idea is short enough to hold in your head.

The problem

A 2 x 2 square moves diagonally across a rectangular screen. Its position is represented by the row and column of its top-left cell.
On every step, the square moves one row and one column in its current direction. If that move would cross a horizontal or vertical screen boundary, the corresponding direction component reverses before the square moves.
The input direction is one of "up-left", "up-right", "down-left", or "down-right".
Return the minimum number of steps until the square covers any corner of the screen. The starting position counts, so return 0 when the square already covers a corner. If the square will never cover a corner, return -1.

Function
stepsToScreenCorner(size: int[], position: int[], direction: String) → int

Examples
Example 1
size = [6,18]
position = [3,1]
direction = "up-right"
return = 15
The square repeatedly reflects while its top-left cell stays within rows 0 through 4 and columns 0 through 16. After 15 moves, it first reaches a top-left position that makes the square cover a screen corner.
Example 2
size = [6,6]
position = [1,1]
direction = "up-right"
return = -1
The position-and-direction state eventually repeats without covering any screen corner, so no future step can succeed.

Constraints
size.length = 2 and both dimensions are at least 2.
position.length = 2.
0 <= position[0] <= size[0] - 2.
0 <= position[1] <= size[1] - 2.
direction is one of the four supported diagonal direction strings.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Shrink the screen to the top-left cell's range. Rows go 0 to R-2 and columns 0 to C-2, so the square covers a corner exactly when the top-left lands on row 0 or R-2 and column 0 or C-2. Each axis bounces independently, with period 2*(R-2) and 2*(C-2). Simplest safe approach: simulate step by step, checking for a corner at each position, and stop when the (row, col, direction) state repeats. That gives -1. The state space is bounded by about 4*R*C, so it terminates. The pitfall is the reversal rule. Direction flips before the move if the move would leave bounds, so check the next cell, not the current one. Another trap is forgetting step 0 counts. The faster route is unfolding each axis and solving for a common time with modular arithmetic, but simulation with a visited set is enough. StealthCoder is the hedge if you blank on the reversal order mid-assessment.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Bouncing Square Reaches a Screen Corner cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Duolingo's OA.

Duolingo reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Bouncing Square Reaches a Screen Corner FAQ

What's the trick in the Duolingo bouncing square problem?+

Treat the 2x2 square as its top-left cell moving in a (R-1) by (C-1) grid. A corner means that cell sits on both a row extreme and a column extreme. Then simulate with reflection and track visited states to catch cycles.

How do I detect the -1 case?+

Store (row, col, direction) in a set after every step. If you see a state again before covering a corner, the path is a loop and no corner will ever come. Return -1. The state count is finite, so this always ends.

Does the starting position count as a step?+

No, it counts as step 0. Check the corner condition before you make any move. If the top-left cell is already at a row extreme and a column extreme, return 0 right away.

Is brute-force simulation fast enough?+

Usually yes, since states are capped near 4 times rows times columns. If the screen is huge, use the per-axis periods 2*(R-2) and 2*(C-2) and look for a shared time. Simulation is the safer first answer and easier to get right.

How do I prepare for this in 48 hours?+

Write the simulation once from scratch. Test the reversal rule on a thin screen like 2 by 2 or 2 by 5, where the square bounces every step. Then test Example 2 to confirm your cycle check returns -1. That covers the edge cases.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Duolingo.

OA at Duolingo?
Invisible during screen share
Get it