Bouncing Square Reaches a Screen Corner
Updated
Problem statement
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) → intExamples
Example 1
size = [6,18]position = [3,1]direction = "up-right"return = 15The 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 = -1The position-and-direction state eventually repeats without covering any screen corner, so no future step can succeed.
Constraints
size.length = 2and both dimensions are at least2.position.length = 2.0 <= position[0] <= size[0] - 2.0 <= position[1] <= size[1] - 2.directionis one of the four supported diagonal direction strings.
More Duolingo interview questions
- Most-Read Page Across Every Valid StorylineOA · PHONE SCREEN · Seen Oct 2022
- Shortest Route Through a Traced MazeOA · Seen Sep 2022
- Rank Common Translation MistakesOA · Seen Sep 2022
- Count Distinct Word MeaningsOA · Seen Sep 2021
- Count Legal Outfit CombinationsOA · Seen Sep 2021
- Group Timestamps into Consecutive-Day IntervalsPHONE SCREEN · Seen Nov 2026
- Return Every Matching DecryptionPHONE SCREEN · Seen Nov 2026
- Visit Desired Attractions Without Reusing a TrailPHONE SCREEN · Seen Oct 2026
- Correct an In-Place Array ReversalPHONE SCREEN · Seen Oct 2026
- Count Starts Reaching a Grid MaximumPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Stacked Item and Category DiscountsPalantir · OAPalantir interview questions
- Fraction to Recurring DecimalAirbnb · OAAirbnb interview questions
- Arithmetic Triplets After QueriesCapital One · OACapital One interview questions
- Fractional Share Inventory FlatteningRobinhood · PHONE SCREEN · ONSITE INTERVIEWRobinhood interview questions
- Group-Relative Policy AdvantagesAmazon · ONSITE INTERVIEW
- Per-Segment SpeedingOnePay · PHONE SCREENOnePay interview questions
- Stock System with Updates and Percentage ChangeLead Bank · ONSITE INTERVIEWLead Bank interview questions
- Transaction Fee Calculator — Channel Volume TiersStripe · PHONE SCREENStripe interview questions
- Pro-Rata Order BookIMC Trading · ONSITE INTERVIEWIMC Trading interview questions
- Organization Reputation After Employee DeparturesHSBC · OAHSBC interview questions