You are on page 1of 4

Additional DS Programming Questions

UNIT-1 LINKED LIST


1. Given a pointer to the head of a singly-linked list, print each value from the reversed
list. If the given list is empty, do not print anything.
2. You’re given the pointer to the head nodes of two linked lists. Compare the data in the
nodes of the linked lists to check if they are equal. If all data attributes are equal and
the lists are the same length, return 1. Otherwise, return 0.
3. You are given the pointer to the head node of a sorted linked list, where the data in the
nodes is in ascending order. Delete nodes and return a sorted list with each distinct
value in the original list. The given head pointer may be null indicating that the list is
empty.
4. Given the pointer to the head node of a doubly linked list, reverse the order of the

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.

UNIT-2 STACKS and QUEUES


1. You are given a stack of N integers. In one operation, you can either pop an element
from the stack or push any popped element into the stack. You need to maximize the
top element of the stack after performing exactly K operations. If the stack becomes
empty after performing K operations and there is no other way for the stack to be non-
empty, print -1.

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

4. Your task is to construct a tower in N days by following these conditions:

 Every day you are provided with one disk of distinct size.


 The disk with larger sizes should be placed at the bottom of the tower.
 The disk with smaller sizes should be placed at the top of the tower.

The order in which tower must be constructed is as follows:

 You cannot put a new disk on the top of the tower until all the larger disks that are
given to you get placed.

Print N lines denoting the disk sizes that can be put on the tower on the ith day.

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

 First line: Two space-separated integers N and Q respectively


 Second line: A string s of length N (where the ith character of s represents
the character stored in node i)
 Next N−1 line: Two space-separated integers u and v denoting an edge between
node u and node v
 Next Q lines: An integer u and a space-separated character c 

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.

3. Given an array A of N integers, classify it as being Good Bad or Average. It is called


Good, if it contains exactly X distinct integers, Bad if it contains less than X distinct
integers and Average if it contains more than X distinct integers.
Input format:
First line consists of a single integer T denoting the number of test cases.
First line of each test case consists of two space separated integers denoting N and X.
Second line of each test case consists of N space separated integers denoting the array
elements.
Output format:
Print the required answer for each test case on a new line.

4. You are given a directory tree of N directories/folders. Each directory is represented


by a particular id which ranges from 1 to N. The id of the root directory is 1, then it
has some child directories, those directories may contain some other ones and it goes
on. Now you are given a list of directory id's to delete, you need to find the minimum
number of directories that need to be deleted so that all the directories in the given list
get deleted. 
Note that if you delete a particular directory, all its child directories will also get
deleted.

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:

a. When a topic is mentioned in a 'Post', its z-score is increased by 50.


b. A 'Like' on such a Post, increases the z-score by 5.
c. A 'Comment' increases z-score by 10.
d. A 'Share' causes an increment of 20.

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.

You might also like