Find the last child to receive a toy in a circle

Quick Overview

Children stand in a circle and a teacher hands out toys one by one, clockwise from a given starting child. Find the position of the child who receives the last toy, with up to a billion children and toys. It tests modular arithmetic on a circle and careful handling of 1-indexed positions.

Find the last child to receive a toy in a circle

Company: Goldman Sachs

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

There are `N` children standing in a circle, numbered from `1` to `N` in clockwise order. A teacher distributes `T` toys, starting from the child at position `D` and giving one toy to each consecutive child in clockwise order (wrapping back to child `1` after child `N`). Return the position (1-indexed) of the last child to receive a toy. ### Function Signature ```python def last_child(n: int, t: int, d: int) -> int: ``` ### Rules - The parameters `n`, `t` and `d` are `N`, `T` and `D` from the statement. - Child `d` receives the first toy. A child can receive more than one toy when `t > n`. - Return a position between `1` and `n` inclusive. ### Constraints - `1 <= n <= 10^9` - `1 <= t <= 10^9` - `1 <= d <= n` ### Examples ```text Input: n = 5, t = 2, d = 1 Output: 2 ``` Toy 1 goes to child 1 and toy 2 goes to child 2, so the last child is 2. ```text Input: n = 5, t = 7, d = 3 Output: 4 ``` Starting at child 3, the toys go to children 3, 4, 5, 1, 2, 3, 4. The seventh and last toy goes to child 4. ```text Input: n = 4, t = 6, d = 3 Output: 4 ``` The toys go to children 3, 4, 1, 2, 3, 4, so the last toy lands on child 4, the highest position in the circle.

Overview: Children stand in a circle and a teacher hands out toys one by one, clockwise from a given starting child. Find the position of the child who receives the last toy, with up to a billion children and toys. It tests modular arithmetic on a circle and careful handling of 1-indexed positions.

Goldman Sachs logo
Goldman Sachs
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

There are N children standing in a circle, numbered from 1 to N in clockwise order. A teacher distributes T toys, starting from the child at position D and giving one toy to each consecutive child in clockwise order (wrapping back to child 1 after child N).

Return the position (1-indexed) of the last child to receive a toy.

Function Signature

def last_child(n: int, t: int, d: int) -> int:

Rules

  • The parameters n , t and d are N , T and D from the statement.
  • Child d receives the first toy. A child can receive more than one toy when t > n .
  • Return a position between 1 and n inclusive.

Constraints

  • 1 <= n <= 10^9
  • 1 <= t <= 10^9
  • 1 <= d <= n

Examples

Input: n = 5, t = 2, d = 1
Output: 2

Toy 1 goes to child 1 and toy 2 goes to child 2, so the last child is 2.

Input: n = 5, t = 7, d = 3
Output: 4

Starting at child 3, the toys go to children 3, 4, 5, 1, 2, 3, 4. The seventh and last toy goes to child 4.

Input: n = 4, t = 6, d = 3
Output: 4

The toys go to children 3, 4, 1, 2, 3, 4, so the last toy lands on child 4, the highest position in the circle.