Extract Server Logs and Find Their Best Shutdown Times
Problem statement
A noisy string may contain zero or more server logs. A valid log is a non-overlapping substring with this exact form:
begin<binary-payload>endThe payload must contain at least one character, and every payload character must be 0 or 1. Text outside valid logs is ignored.
For each valid log, choose a shutdown index using the following penalty rule:
0means the server was running, and1means it was down.- A down slot before shutdown adds one penalty point.
- A running slot at or after shutdown adds one penalty point.
- Shutdown may happen at any index from
0through the payload length.
Return the earliest minimum-penalty shutdown index for every extracted log, in left-to-right order.
Extraction rule for this exercise
Search left to right for the next exact lowercase begin marker followed immediately by one or more binary characters and then the exact lowercase end marker. Matches do not overlap. A malformed marker or payload is noise and does not prevent a later valid match.
What the interview report shared
The reported third phase extracted valid nonnested begin...end logs from larger content and returned the best shutdown position for each binary payload. One preserved framing example contains begin001end.
Function
bestShutdownTimes(content: String) → int[]Examples
Example 1
content = "noisebegin001endxxbegin110end"return = [2,0]The payloads are 001 and 110. Their earliest best shutdown indices are 2 and 0.
Example 2
content = "begin01xend--begin0101end--beginend"return = [1]The first payload contains x, and the final payload is empty, so both are noise. Only 0101 is valid; its earliest best shutdown index is 1.
Constraints
1 <= content.length <= 2 * 10^5.contentcontains printable ASCII characters.- The total length of all extracted binary payloads is at most
content.length. - For this exercise, equal minimum penalties use the earliest shutdown index.