<div dir="auto">Very probably. But under pressure I went with what I knew I could get right.  In hindsight it was like one of those non-quantitative multiple-choice tests where you have to figure out not the correct answer but the answer that the test author is most likely to believe to be correct.</div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">On Mon, Mar 10, 2025, 4:49 PM Bakul Shah <<a href="mailto:bakul@iitbombay.org">bakul@iitbombay.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Perhaps the interviewer was looking for something dumb like the following<br>
and not a full RD parser?<br>
<br>
int count = 0;<br>
while (*cp) {<br>
        char c = *cp++;<br>
        count += c == '(' ? 1 : c == ')' ? -1 : 0;<br>
        if (count < 0) return -1; // FAIL: one too many )<br>
}<br>
if (count > 0) return -1; // FAIL: too many (<br>
return 0; // SUCCESS<br>
<br>
Though this will fall apart if you also want to also balance braces &/or<br>
brackets and must catch invalid cases like "(..[..)..]"!<br>
<br>
> On Mar 10, 2025, at 8:19 AM, John Cowan <<a href="mailto:cowan@ccil.org" target="_blank" rel="noreferrer">cowan@ccil.org</a>> wrote:<br>
> <br>
> I was working at the whiteboard during a job interview once. I had been asked to write a function to report if its input had balanced parentheses.  No problem: I wrote an RD parser in Python (which I prefer for whiteboarding) to detect balance and return True if the parse was successful and False if EOF was reached.<br>
> <br>
> I was starting to write some tests when the interviewer interrupted me.<br>
> <br>
> "What is that?"<br>
> <br>
> "It's a recursive descent parser. It detects if the input is well-formed."<br>
> <br>
> Blank look.<br>
> <br>
> I started to walk him through the code.<br>
> <br>
> He interrupted me. "Excuse me, I'll be back in a few minutes."<br>
> <br>
> Long wait, maybe 15-20 minutes. Someone else comes in. "Thank you, the recruiter will get back to you." That's the last I hear from them.<br>
<br>
</blockquote></div>