Palindrome number and merge intervals
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Google's first round (coding), last asked Mar 2026. It tests math, intervals and sorting.
Two short, independent problems.
Part 1: palindrome number
Decide whether an integer reads the same from left to right and from right to left. Negative numbers are not palindromes, because of the minus sign. Try to do it without converting the number to a string.
Unless you agree on a different interface with the interviewer, assume:
is_palindrome(x: int) -> bool
is_palindrome(121) -> True
is_palindrome(-121) -> False
is_palindrome(10) -> False
is_palindrome(0) -> True
Part 2: merge intervals
Given intervals [start, end] in any order, merge the overlapping ones and return non-overlapping intervals that cover exactly the same points, sorted by start. Intervals that share a single point overlap.
merge(intervals: list[list[int]]) -> list[list[int]]
merge([[1,3], [2,6], [8,10], [15,18]]) -> [[1,6], [8,10], [15,18]]
merge([[1,4], [4,5]]) -> [[1,5]]
merge([]) -> []
Constraints
- Part 1:
xfits in a signed 32-bit integer. - Part 2: up to 10^4 intervals;
-10^9 <= start <= end <= 10^9.
Clarifying questions
- Part 1: may I convert to a string? Must I guard against overflow when reversing?
- Part 2: do touching intervals merge? Is the input sorted? May I modify it?
What a strong answer covers
- Part 1: reverse only the second half of the digits until the reversed part is at least the remaining part, then compare (
x == revorx == rev // 10for odd lengths). This avoids overflow. Numbers ending in 0 (other than 0) are never palindromes. - Part 2: sort by start, then one pass that extends the last merged interval while the next start is
<= its end.O(n log n)time. - Tests: 0, single digits, numbers ending in zero; an empty list, nested intervals, touching intervals, unsorted input.
Follow-ups
- Part 1 for a number given in base
b, or for a linked list of digits. - Part 2: insert one new interval into an already merged sorted list in
O(n). - Part 2: return the total length covered, or the gaps between intervals.
Where it's asked
- Google · First round: coding · Last asked Mar 202645 min · Coding in a Google Doc, no syntax highlighting or autocomplete · No AI
What it tests
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.
