Minimum Markers to Clear Line Segments
Reported by candidates from HSBC's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
HSBC reported this one in November 2025, and the title hides what it really is. "Minimum markers to clear line segments" is interval stabbing: find the fewest points so every closed interval contains at least one. If you've seen the burst-balloons-with-arrows problem, you've seen this. The wrinkle is that endpoints come unordered, so you normalize each pair first. If your OA is in a day or two, the algorithm is short and the traps are small. StealthCoder sits invisibly as a safety net on the live assessment if you blank, but you can own this one before then.
The problem
You are given two integer arrays startX and endX of equal length. Pair i describes one closed line segment on the X-axis. Normalize each pair before using it: The segment's left endpoint is min(startX[i], endX[i]). The segment's right endpoint is max(startX[i], endX[i]). Placing a marker at an X-axis coordinate clears every remaining segment that contains that coordinate, including segments that meet it at an endpoint. Return the minimum number of marker placements needed to clear all segments. Return 0 when both arrays are empty. Function markerPlaced(startX: int[], endX: int[]) → int Examples Example 1 startX = [0, 2, 4, -8] endX = [4, 5, 8, -9] return = 2 The normalized segments are [0, 4], [2, 5], [4, 8], and [-9, -8]. A marker at 4 clears the first three segments. A second marker at either -9 or -8 clears the remaining segment. No single coordinate belongs to both the negative segment and the three nonnegative segments, so the minimum is 2. Constraints 0 <= startX.length = endX.length <= 10^4 -10^9 <= startX[i], endX[i] <= 10^9 Every endpoint is an integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Normalize each pair to [min, max]. Sort the segments by right endpoint. Place a marker at the first segment's right endpoint. Then walk forward, and any segment whose left endpoint is less than or equal to the current marker is already cleared. When you hit a segment whose left endpoint is greater than the marker, place a new marker at that segment's right end and increment the count. Greedy works because the earliest right endpoint is the best spot to cover the most overlapping segments without losing any. The common pitfall is using strict less-than. These are closed segments, so touching at 4 counts as cleared. Another miss is forgetting to normalize, as in the example where -8 and -9 arrive reversed. Handle empty input by returning 0. Sorting dominates at O(n log n), which is fine for 10^4 segments. If you freeze during the live OA, StealthCoder can surface this greedy for you.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Minimum Markers to Clear Line Segments cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as minimum number of arrows to burst balloons. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass HSBC's OA.
HSBC reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Minimum Markers to Clear Line Segments FAQ
What's the trick in the HSBC minimum markers problem?+
It's interval stabbing. Normalize each pair into [min, max], sort by right endpoint, and greedily place a marker at the right end of the first uncovered segment. Skip every segment whose left end is at or before that marker. Count the placements.
Do touching segments share a marker?+
Yes. The segments are closed, and the statement says a marker clears segments meeting it at an endpoint. In example 1, [0,4], [2,5], and [4,8] are all cleared by a marker at 4. Use less-than-or-equal when comparing a left endpoint to the last marker.
Why sort by right endpoint instead of left?+
Sorting by right endpoint lets you put the marker as far right as possible while still covering the current segment. That covers the most later segments. Sorting by left can make you place markers too early and overcount.
How hard is this really?+
Easy to medium. The code is about ten lines once you spot the greedy. The difficulty is recognizing it as interval stabbing and remembering to normalize reversed endpoints. Edge cases are empty arrays and a single segment.
How do I prepare in 48 hours?+
Write the greedy from scratch twice, including normalization and the empty case. Then test on example 1 and on a case with negatives and duplicates. Also review the arrows-to-burst-balloons problem, since it's the same idea. That's enough for this question.