Code RoomMail merge nesting
EasyPrep Room Coding #4773

Mail merge nesting

CodingAlgorithms & data structuresEntry–Mid~14 min

A mail merge template is compiled from a flat list of block markers. Every entry in markers reads either "open|loop" or "close|loop", where the text after the pipe is the block name, and block names may repeat. A template compiles when each open is closed by a close carrying the same name, and blocks close in the reverse order they opened, so a block opened inside another must close before the outer one does. Return -1 when the whole list compiles. Otherwise return the position of the first entry the compiler cannot accept, meaning a close that names something other than the innermost block still open, or a close arriving while no block is open. When the list runs out with blocks still open, return the position of the earliest marker still open. An empty list compiles.

Implement
template_nesting_fault(markers: list[str]) → int
Examples
in[["open|loop","open|if","close|if","close|loop"]]out-1
in[["open|loop","close|if"]]out1
in[["open|header","open|row"]]out0
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.

0:00 of about 14 min
InputExpectedGot
[["open|loop","open|if","close|if","close|loop"]]-1not run yetsample
[["open|loop","close|if"]]1not run yetsample
[["open|header","open|row"]]0not run yetsample