OLAP Vision Interview Question

Country: United States
Interview Type: Phone Interview

Comment hidden because of low score. Click to expand.
of 0 vote

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)

from collections import deque

def main():
    test("[ytu[78]][12]", 0) # should be 8
    test("[ytu[78]][12]", 4) # should be 7
    test("[ytu[78]][12]", 9) # should be 12
    test("[ytu[78]][12]", 1) # No matching bracket

def test(s, i):
    matching_index = get_index_of_matching_bracket(s, i)
    print(s + ", " + str(i) + ": " + str(matching_index))

def get_index_of_matching_bracket(s, i):

    if s[i] != '[':
        return -1

    d = deque()

    for k in range(i, len(s)):
        if s[k] == ']':
        elif s[k] == '[':

        if not d:
            return k

    return -1

if __name__ == "__main__":

- havanagrawal November 15, 2017 | Flag Reply

Add a Comment

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.


is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More


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.

Learn More

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.

Learn More

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.

Learn More