Minimum Points Hitting Every Closed Interval
Updated
Problem statement
You are given a list of integer intervals [left,right]. Each interval includes both of its endpoints. Choose as few integer points as possible so that every interval contains at least one chosen point.
More than one set of points can be the smallest. Return the set this greedy method builds, in increasing order:
- Sort the intervals by right endpoint, smallest first. Break ties by left endpoint, smallest first.
- Go through the intervals in that order. If the current interval holds no chosen point yet, choose its right endpoint.
Function
minimumHittingPoints(intervals: int[][]) → int[]Examples
Example 1
intervals = [[1,3],[2,5],[3,6]]return = [3]Point 3 lies in every interval.
Example 2
intervals = [[1,2],[3,4],[2,3]]return = [2,4]The first interval with no point yet gets point 2. The later interval [3,4] still has no point, so it gets point 4.
Example 3
intervals = []return = []There are no intervals, so no points are needed.
Constraints
0 <= intervals.length <= 200000.-10^9 <= left <= right <= 10^9.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Minimum Railway PlatformsMakeMyTrip.com · ONSITE INTERVIEW
- Maximum Non-Overlapping IntervalsMicrosoft · ONSITE INTERVIEW
- Minimum Removals for Non-Overlapping IntervalsByteDance · PHONE SCREEN · ONSITE INTERVIEW
- Minimum Workers with Job AssignmentsLyft · ONSITE INTERVIEW
- Minimum Removals for Non-Overlapping IntervalsBloomberg LP · ONSITE INTERVIEW
- Maximum Compatible Time WindowsIBM · OA
- Task SchedulingJP Morgan Chase · OA
- Maximum Number of Events That Can Be AttendedSnowflake · PHONE SCREEN
- Minimum Appointment Cancellations with a Required BreakMaven Clinic · PHONE SCREEN
- Minimum Markers to Clear Line SegmentsHSBC · OA