Code Room
CodingMediumcod-g1301
Subject Prefix sumLevel Entry–Mid~16 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

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) → int
Examples
in["IOOI"]out4
What 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.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.