Вы находитесь на странице: 1из 5

1.

Consider the usual algorithm for determining whether a sequence of parentheses is


balanced. What is the maximum number of parentheses that will appear on the stack AT
ANY ONE TIME when the algorithm analyzes: (()(())(()))
2. Interleave the first half of the queue with second half : Given a queue of integers of
even length, rearrange the elements by interleaving the first half of the queue with the
second half of the queue. Only a stack can be used as an auxiliary space.

Examples:

Input : 1 2 3 4
Output : 1 3 2 4
Input : 11 12 13 14 15 16 17 18 19 20
Output : 11 16 12 17 13 18 14 19 15 20

3. Remove brackets from an algebraic string containing + and – operators : Simplify a


given algebraic string of characters, ‘+’, ‘-‘ operators and parentheses. Output the
simplified string without parentheses.

Examples:

Input : "a-(b+c)"
Output : "a-b-c"
4. Check if stack elements are pairwise consecutive

Given a stack of integers, write a function pairWiseConsecutive() that checks whether


numbers in the stack are pairwise consecutive or not. The pairs can be increasing or
decreasing, and if the stack has an odd number of elements, the element at the top is left
out of a pair. The function should retain the original stack content.

Examples:

Input : stack = [4, 5, -2, -3, 11, 10, 5, 6, 20]


Output : Yes
Each of the pairs (4, 5), (-2, -3), (11, 10) and
(5, 6) consists of consecutive numbers.

Input : stack = [4, 6, 6, 7, 4, 3]


Output : No - (4, 6) are not consecutive
5. Sort a stack using a temporary stack : Given a stack of integers, sort it in ascending
order using another temporary stack.
6. How many stacks are needed to implement a queue. Consider the situation where no
other data structure like arrays, linked list is available to you.

7. How many queues are needed to implement a stack. Consider the situation where no
other data structure like arrays, linked list is available to you.
8. Explain the functionality of following functions.

Question 1

int fun1(int x, int y)


{
if(x == 0)
return y;
else
return fun1(x - 1, x + y);
}
9. Predict the output of following program.

#include<stdio.h>
void fun(int x)
{
if(x > 0)
{
fun(--x);
printf("%d\t", x);
fun(--x);
}
}

int main()
{
int a = 4;
fun(a);
getchar();
return 0;
}
.
Data Structures Tutorial2 with solution

Interleave the first half of the queue with second half

Given a queue of integers of even length, rearrange the elements by interleaving the first half of
the queue with the second half of the queue.

Only a stack can be used as an auxiliary space.

Examples:

Input : 1 2 3 4
Output : 1 3 2 4

Input : 11 12 13 14 15 16 17 18 19 20
Output : 11 16 12 17 13 18 14 19 15 20

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Following are the steps to solve the problem:


1.Push the first half elements of queue to stack.
2.Enqueue back the stack elements.
3.Dequeue the first half elements of the queue and enqueue them back.
4.Again push the first half elements into the stack.
5.Interleave the elements of queue and stack.

Remove brackets from an algebraic string containing + and – operators

Simplify a given algebraic string of characters, ‘+’, ‘-‘ operators and parentheses. Output the
simplified string without parentheses.

Examples:

Input : "a-(b+c)"
Output : "a-b-c"

Input : "a-(b-c-(d+e))-f"
Output : "a-b+c+d+e-f"

The idea is to check operators just before starting of bracket, i.e., before character ‘(‘. If operator
is -, we need to toggle all operators inside the bracket. A stack is used which stores only two
integers 0 and 1 to indicate whether to toggle or not.
We iterate for every character of input string. Initially push 0 to stack. Whenever the character is
an operator (‘+’ or ‘-‘), check top of stack. If top of stack is 0, append the same operator in the
resultant string. If top of stack is 1, append the other operator (if ‘+’ append ‘-‘) in the resultant
string.

Check if stack elements are pairwise consecutive

Given a stack of integers, write a function pairWiseConsecutive() that checks whether numbers
in the stack are pairwise consecutive or not. The pairs can be increasing or decreasing, and if the
stack has an odd number of elements, the element at the top is left out of a pair. The function
should retain the original stack content.

Only following standard operations are allowed on stack.

 push(X): Enter a element X on top of stack.


 pop(): Removes top element of the stack.
 empty(): To check if stack is empty.

Examples:

Input : stack = [4, 5, -2, -3, 11, 10, 5, 6, 20]


Output : Yes
Each of the pairs (4, 5), (-2, -3), (11, 10) and
(5, 6) consists of consecutive numbers.
Input : stack = [4, 6, 6, 7, 4, 3]
Output : No
(4, 6) are not consecutive.

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The idea is to use another stack.

1. Create an auxiliary stack aux.


2. Transfer contents of given stack to aux.
3. Traverse aux. While traversing fetch top two elements and check if they are consecutive
or not. After checking put these elements back to original stack.

Sort a stack using a temporary stack: Given a stack of integers, sort it in ascending order
using another temporary stack.

Examples:

Input : [34, 3, 31, 98, 92, 23]


Output : [3, 23, 31, 34, 92, 98]

Input : [3, 5, 1, 4, 2, 8]
Output : [1, 2, 3, 4, 5, 8]

Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

We follow this algorithm.

1. Create a temporary stack say tmpStack.


2. While input stack is empty do this:
o Pop an element from input stack call it x
o while temporary stack is not empty and top of stack is greater than x,
pop from temporary stack and push it to the input stack
o push temp in tempory of stack
3. The sorted numbers are in tmpStack

How many stacks are needed to implement a queue. Consider the situation where no other data
structure like arrays, linked list is available to you.

How many queues are needed to implement a stack. Consider the situation where no other data
structure like arrays, linked list is available to you.