## OLAP Vision Interview Question

Country: United States
Interview Type: Phone Interview

Comment hidden because of low score. Click to expand.
0
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] == ']':
d.popleft()
elif s[k] == '[':
d.append(s[i])

if not d:
return k

return -1

if __name__ == "__main__":
main()``````

Name:

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

### Books

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

### 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.