Longest balanced entry exit stretch
A ward's door sensor writes one character per event: 'I' when someone enters, 'O' when someone leaves. For a staffing report, find the length of the longest contiguous stretch of events containing exactly as many entries as exits. Return 0 if no such stretch exists. The input contains only 'I' and 'O' characters and may be empty. Example: events = "IOOI" gives 4, since the whole log has two entries and two exits.
Implement
longest_balanced_stretch(events: str) → intExamples
in
["IOOI"]out4What a strong answer looks like
State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
0:00 of about 16 min
solution.py
InputExpectedGot
["IOOI"]4not run yetsample