If there are going to be multiple queries against the same string, it would be wiser to first calculate all the matching pairs in a hash, and then have amortized O(1) time complexity for a query.

On the other hand, if there are going to be multiple queries against different strings, then you can use the approach below.

Potential things to consider:1. How should the code behave when the input index is not an opening bracket? (raise exception vs return some sentinel value)2. How would you handle more types of bracket pairs (, [, {, <, etc.?3. Can you do it without a stack for one-time queries? (yes)

What's Going On

Books

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Resume Review

Most engineers make critical mistakes on their resumes -- we can
fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.