Data Structure Questions and Answers-Decimal to Binary using Stacks

YOU CAN DOWNLOAD 200+ SUBJECTS PDF BOOK FOR COMPETITIVE EXAMINATIONS

CLICK HERE TO DOWNLOAD

Data Structure Questions and Answers-Decimal to Binary using Stacks

Question 6 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
Write a piece of code which returns true if the string contains balanced parenthesis, false otherwise.
A

public boolean isBalanced(String exp) { int len = exp.length(); Stack<Integer> stk = new Stack<Integer>(); for(int i
B

public boolean isBalanced(String exp) { int len = exp.length(); Stack<Integer> stk = new Stack<Integer>(); for(int i <
C

public boolean isBalanced(String exp) { int len = exp.length(); Stack<Integer> stk = new Stack<Integer>(); for(int i <
D

public boolean isBalanced(String exp) { int len = exp.length(); Stack<Integer> stk = new Stack<Integer>(); for(int i <
Question 6 Explanation: 
Whenever a '(' is encountered, push it into the stack, and when a ')' is encountered check the top of the stack to see if there is a matching '(', if not return false, continue this till the entire string is processed and then return true.

Question 7 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
What is the time complexity of the above code?
A
O(logn)
B
O(n)
C
O(1)
D
O(nlogn)
Question 7 Explanation: 
All the characters in the string have to be processed, hence the complexity is O(n).

Question 8 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
For every matching parenthesis, print their indices.
A

public void dispIndex(String exp) { Stack<Integer> stk = new Stack<Integer>(); for (int i = 0; i < len; i++
B

public void dispIndex(String exp) { Stack<Integer> stk = new Stack<Integer>(); for (int i = 0; i < len; i++
C

public void dispIndex(String exp) { Stack<Integer> stk = new Stack<Integer>(); for (int i = 0; i < len; i++
D
None of the mentioned
Question 8 Explanation: 
Whenever a '(' is encountered, push the index of that character into the stack, so that whenever a corresponding ')' is encountered, you can pop and print it.

There are 8 questions to complete.