Check Digits and Combine Ranges
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This two-part question evaluates numeric digit-manipulation and interval-merging competencies, specifically integer palindrome detection (accounting for sign and integer limits) and combining overlapping ranges via sorting and range aggregation in the Coding & Algorithms domain.
Part 1: Check Whether an Integer Reads the Same Backward
Constraints
- -2^31 <= x <= 2^31 - 1
- Negative integers must return False
- An arithmetic-based solution is preferred over string conversion
Examples
Input: 121
Expected Output: True
Explanation: 121 reads the same forward and backward.
Input: -121
Expected Output: False
Explanation: Negative numbers are not palindromes.
Input: 10
Expected Output: False
Explanation: Reversed, 10 becomes 01, which is not equal to 10.
Input: 0
Expected Output: True
Explanation: 0 is the same when read from both directions.
Input: 12321
Expected Output: True
Explanation: The digits mirror around the center.
Hints
- A negative number can never be a palindrome because the minus sign only appears on the left.
- Try reversing only the second half of the digits instead of the whole number.
Part 2: Combine Overlapping Ranges
Constraints
- 0 <= len(intervals) <= 10^4
- -10^9 <= start <= end <= 10^9
- Intervals may be unsorted
Examples
Input: [[1, 3], [2, 6], [8, 10], [15, 18]]
Expected Output: [[1, 6], [8, 10], [15, 18]]
Explanation: The first two intervals overlap and merge into [1, 6].
Input: [[1, 4], [4, 5]]
Expected Output: [[1, 5]]
Explanation: The intervals share the point 4, so they overlap.
Input: []
Expected Output: []
Explanation: An empty input produces an empty result.
Input: [[5, 7]]
Expected Output: [[5, 7]]
Explanation: A single interval does not need merging.
Input: [[1, 4], [0, 2], [3, 5]]
Expected Output: [[0, 5]]
Explanation: After sorting, all three intervals connect through overlap and merge into one.
Hints
- Sort the intervals by their start value before trying to merge them.
- Keep track of the last interval in the output. If the next interval overlaps, extend the end; otherwise, start a new interval.