First meeting point of two digit-sum sequences
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in OpenAI's agentic coding (beta) round, last asked Mar 2026. It tests math and two pointers.
Start from a positive integer and repeatedly add the sum of its decimal digits to get the next value: after 471 comes 471 + 4 + 7 + 1 = 483. Given the starting values of two such sequences, return the smallest value that appears in both. Each starting value belongs to its own sequence, and the two sequences are guaranteed to share a value below 20,000,000.
Interface
Unless you agree on a different interface with the interviewer, assume:
first_common(s1: int, s2: int) -> int
Example
first_common(471, 480) -> 519
# 471, 483, 498, 519, ...
# 480, 492, 507, 519, ...
first_common(91, 100) -> 101 # 91 + 9 + 1 = 101 and 100 + 1 = 101
first_common(57, 57) -> 57 # same start
Constraints
-
1 <= s1, s2 < 20,000,000. - A common value below 20,000,000 exists.
Clarifying questions
- Is a common value always guaranteed, or should the function detect that there is none?
- Does the starting value count as a member of its sequence?
- How large can the values get, and is there a time limit per call?
What a strong answer covers
- Both sequences strictly increase, so walk them like a merge: advance whichever current value is smaller until they are equal.
- Why this finds the first common value: no value is skipped on the side that is behind.
- Time proportional to the number of steps taken, O(1) memory; the alternative of storing one sequence in a set and the memory it costs.
- A fast digit sum (repeated
% 10) and tests for equal starts, one start inside the other sequence, and small values.
Follow-ups
- No meeting point is guaranteed. How do you stop, and what bound do you use?
- Many queries for pairs of starts under a fixed limit. What would you precompute?
- Find the first value shared by
ksequences at once.
Where it's asked
- OpenAI · Agentic coding (beta) · Last asked Mar 2026An existing codebase and changes too large to write by hand; you work through them with an AI coding agent · An AI coding agent is expected
What it tests
math · two pointers
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
