# LinkendIn Phone Interview Question on 09/23

• This function determines if the braces ('(' and ')') in a string are properly matched.

Some examples:
"()()()()" -> true
"((45+)*a3)" -> true
"(((())())" -> false

`````` public boolean matched(String s) {
// Implementation here
}

``````

Description: Given a binary tree, write a method to print the tree level by level.
(e.g)
Tree:
1
/
3 5
/ \
2 4 7
/ \
9 6 8

==========
Expected Output:
1
3 5
2 4 7
9 6 8

``````class Node {
Node left;
Node right;
int val;
}

void printBinaryTree(Node root) {
}
111``````

• Hello, these questions seem very simple.
Is this like the first phase of all technical interviews?
Also, where did you see(or listened through phone?) these questions? Where did you write the code and was there a on-line judge?
Thanks, I am also preparing for interviews

• simple counter based approach

``````def matched(s):
cnt = 0
if s:
for c in s:
if c == '(':
cnt += 1
if c == ')':
cnt -= 1
if c < 0:
break
return cnt == 0
``````

``````def bfs(root):
levels = []
if root:
q = []
level = []
q.append(root)
q.append(None)
while q:
x = q.pop()
if x is None:
levels.append(level[:])
level = []
if q:
q.append(x)
else:
level.append(x.val)
if x.left:
q.append(x.left)
if x.right:
q.append(x.right)

for level in levels:
print(level)
``````

• @parvez.h.shaikh said in LinkendIn Phone Interview Question on 09/23:

``````def matched(s):
cnt = 0
if s:
for c in s:
if c == '(':
cnt += 1
if c == ')':
cnt -= 1
return cnt == 0
``````

your code will return `true` for string `"))(("`

may be better:

``````def matched(s):
cnt = 0
if s:
for c in s:
if c == '(':
cnt += 1
if c == ')':
cnt -= 1
if cnt < 0:
return False
return cnt == 0
``````

• Hey did you move to onsite rounds at linkedin? what role and how was your experience onsite, please share

• @xuthus thanks edited my code to rectify missing check for negative

• This post is deleted!

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.