Professional Documents
Culture Documents
nodes in place. That is, change the next and prev pointers of the nodes so that the
direction of the list is reversed. Return a reference to the head node of the reversed
list.
Note: The head node might be NULL to indicate that the list is empty.
5. A linked list is said to contain a cycle if any node is visited more than once while
traversing the list. Given a pointer to the head of a linked list, determine if it contains
a cycle. If it does, return 1. Otherwise, return 0.
2. Alice is rearranging her library. She takes the innermost shelf and reverses the order
of books. She breaks the walls of the shelf. In the end, there will be only books and no
shelf walls. Print the order of books.
Opening and closing walls of shelves are shown by '/' and '\' respectively whereas
books are represented by lower case alphabets.
3. A and B are playing a game. In this game, both of them are initially provided with
a list of n numbers. (Both have the same list but their own copy).
Now, they both have a different strategy to play the game. A picks the element
from start of his list. B picks from the end of his list.
You need to generate the result in form of an output list.
Method to be followed at each step to build the output list is:
a) If the number picked by A is bigger than B then this step's output is 1 . B removes the
number that was picked from their list.
b) If the number picked by A is smaller than B then this step's output is 2. A removes the
number that was picked from their list.
c) If both have the same number then this step's output is 0 . Both A and B remove the
number that was picked from their list.
This game ends when at least one of them has no more elements to be picked i.e.
when the list gets empty
You cannot put a new disk on the top of the tower until all the larger disks that are
given to you get placed.
5. In this challenge, you must first implement a queue using two stacks. Then
process queries, where each query is one of the following types:
a: Enqueue element x into the end of the queue.
b: Dequeue the element at the front of the queue.
c: Print the element at the front of the queue.
UNIT 3- TREES
1. You are given a rooted tree that contains N nodes. Each node contains a lowercase
alphabet.
You are required to answer Q queries of type u,c, where u is an integer and c is a
lowercase alphabet. The count of nodes in the subtree of the node u containing c is
considered as the answer of all the queries.
Input format
Output format
For each query, print the output in a new line.
2. You are given an array A of size N. You can perform an operation in which you will
remove the largest and the smallest element from the array and add their difference
back into the array. So, the size of the array will decrease by 1 after each oepration.
You are given Q tasks and in each task, you are given an integer K. For each task,
you have to tell sum of all the elements in the array after K operations.
Input:
First line contains two space-separated integers N and Q, denoting the number of
elements in array and number of queries respectively.
Next line contains N space-separated integers denoting elements of the array.
Next Q lines contain a single integer K.
Output:
For each task, print answer in a new line.
Input
The first line of input contains an integer N that denotes how many folders are there.
The next line contains N space separated integers that where the ith integer denotes
the id of the parent of the directory with id i . Note that the first integer will
be −1 as 1 is the id of root folder and it has no parent. Rest of the integers will not
be −1 .
The next line contains an integer M that denotes how many directories you need to
delete.
The next line contains M space separated integers that denote the ids of the directories
you need to delete.
Output
Print the minimum number of directories that need to be deleted.
5. Roy is trying to develop a widget that shows Trending Topics (similar to Facebook)
on the home page of a website. He has gathered a list of N Topics (their IDs) and their
popularity score (say z-score) from the database. Now z-score change everyday
according to the following rules:
Now the Trending Topics are decided according to the change in z-score. One with
the highest increment comes on top and list follows.
Roy seeks your help to write an algorithm to find the top 5 Trending Topics.
If change in z-score for any two topics is same, then rank them according to their ID
(one with higher ID gets priority). It is guaranteed that IDs will be unique.
Input format:
First line contains integer N
N lines follow
Each contains 6 space separated numbers representing Topic ID, current z-score - Z,
Posts - P, Likes - L, Comments - C, Shares - S
Output format:
Print top 5 Topics each in a new line.
Each line should contain two space separated integers, Topic ID and new z-score of
the topic.