0% found this document useful (0 votes)
10 views4 pages

Datastructures Labexam1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

Datastructures Labexam1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Data Structures : LAB Exam -1

1. You are given N elements and your task is to Implement a Stack in which you can get a
minimum element in O(1) time.
Example 1:
Input:
push(2)
push(3)
pop()
getMin()
push(1)
getMin()
Output: 2 1
Explanation: In the first test case for query
push(2) Insert 2 into the stack. The stack will be 2
push(3) Insert 3 into the stack. The stack will be 2 3
pop() Remove top element from stack Poped element will be 3 the stack will be 2
getMin() Return the minimum element min element will be 2
push(1) Insert 1 into the stack. The stack will be 2 1
getMin() Return the minimum element min element will be 1

Your Task:

You are required to implement the three methods push() which takes one argument an integer
’x’ to be pushed into the stack, pop() which returns an integer popped out from the stack,
and getMin() which returns the min element from the stack. (-1 will be returned if for pop()
and getMin() the stack is empty.)

Expected Time Complexity:

O(1) for all the 3 methods.

Expected Auxiliary Space:

O(n).

1
2. A celebrity is a person who is known to all but does not know anyone at a party. If you go
to a party of N people, find if there is a celebrity in the party or not.
A square NxN matrix M[][] is used to represent people at the party such that if an element of
row i and column j is set to 1 it means ith person knows jth person. Here M[i][i] will always
be 0. Note: Follow 0 based indexing.
Example 1:
Input:
N=3
 
0 1 0
M[][] = 0 0 0
0 1 0
Output: 1
Explanation: 0th and 2nd person both know 1. Therefore, 1 is the celebrity.
Example 2:
Input:
N=2
 
0 1
M[][] =
1 0
Output: -1
Explanation: The two people at the party both know each other. None of them is a celebrity.

Your Task:

Implement the function celebrity() which takes the matrix M and its size N as input param-
eters and returns the index of the celebrity. If no such celebrity is present, return -1.

Expected Time Complexity: O(N)

Expected Auxiliary Space: O(N)

2
3. Find the middle of a given linked list : Given a singly linked list, find the middle of the linked
list. For example, if the given linked list is 1− > 2− > 3− > 4− > 5 then the output should
be 3. If there are even nodes, then there would be two middle nodes, we need to print the
second middle element. For example, if the given linked list is 1− > 2− > 3− > 4− > 5− > 6
then the output should be 4.
You are not allowed to write two for loops for implementing this.

Expected Time Complexity: O(N)

Expected Auxiliary Space: O(1)

4. There are two singly linked lists in a system with size M and N. By some programming error,
the end node of one of the linked lists got linked to the second list, forming an inverted
Y-shaped list. Write a program to get the point where two linked lists merge.

Expected Time Complexity: O(M+N)

Expected Auxiliary Space: O(1)

3
5. Your task is to implement 2 stacks in one array efficiently.That means if you are using only
one of the two stacks then its capacity should be same as the size of the array. You need to
implement 4 methods.
push1 : pushes element into first stack.
push2 : pushes element into second stack.
pop1 : pops element from first stack and returns the popped element. If first stack is empty,
it should return -1.
pop2 : pops element from second stack and returns the popped element. If second stack is
empty, it should return -1.
Example 1:
Input:
push1(2), push1(3), push2(4) ,pop1(), pop2(), pop2()
Output:
3 4 -1
Explanation:
push1(2) the stack1 will be 2
push1(3) the stack1 will be 2,3
push2(4) the stack2 will be 4
pop1() the poped element will be 3 from stack1 and stack1 will be 2
pop2() the poped element will be 4 from stack2 and now stack2 is empty
pop2() the stack2 is now empty hence returned -1.

Expected Time Complexity:

O(1) for all the four methods.

Expected Auxiliary Space:

O(1) for all the four methods.

You might also like