You are on page 1of 6

Coding test:

MCQ:
1. What is the time complexity to count the number of elements in the
linked list?
a) O(1)
b) O(n)
c) O(logn)
d) O(n2)
2. Which of these is not an application of linked list?
a) To implement file systems
b) For separate chaining in hash-tables
c) To implement non-binary trees
d) Random Access of elements
3. In a circular linked list
a) Components are all linked together in some sequential manner.
b) There is no beginning and no end.
c) Components are arranged hierarchically.
d) Forward and backward traversal within the list is permitted.
4. The following function reverse() is supposed to reverse a singly linked
list. There is one line missing at the end of the function.

/* Link list node */


struct node
{
int data;
struct node* next;
};

/* head_ref is a double pointer which points to head (or start) pointer


of linked list */
static void reverse(struct node** head_ref)
{
struct node* prev = NULL;
struct node* current = *head_ref;
struct node* next;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
/*ADD A STATEMENT HERE*/
}
What should be added in place of “/*ADD A STATEMENT HERE*/”, so
that the function correctly reverses a linked list.
a) *head_ref = prev;
b) *head_ref = current;
c) *head_ref = next;
d)*head_ref=NULL;

5. What is the output of following function for start pointing to first node
of following linked list?
1->2->3->4->5->6
void fun(struct node* start)
{
if(start == NULL)
return;
printf("%d ", start->data);
if(start->next != NULL )
fun(start->next->next);
printf("%d ", start->data);
}
a) 1 4 6 6 4 1
b) 1 3 5 1 3 5
c) 1 2 3 5
d) 1 3 5 5 3 1
6. Which of the following points is/are true about Linked List data
structure when it is compared with array
a) Arrays have better cache locality that can make them better in terms
of performance.
b) It is easy to insert and delete elements in Linked List
c) Random access is not allowed in a typical implementation of Linked
Lists
d)The size of array has to be pre-decided, linked lists can change their
size any time.
e) All Of the above.

7. The concatenation of two lists is to be performed in O(1) time. Which


of the following implementations of a list should be used?
a) Singly linked list
b) Doubly linked list
c) Circular linked list
d) Array implementation of list
8. In linked list each node contain minimum of two fields. One field is
data field to store the data second field is?

a. Pointer to character
b. Pointer to integer
c. Pointer to node
d. Node

9. Linked list is considered as an example of ___________ type of


memory allocation.
a. Dynamic
b. Static
c. Compile time
d. None of the mentioned

10.In Linked List implementation, a node carries information regarding


a. Data
b. Link
c. Data and Link
d. None of the mentioned

11.The following steps in a linked list


p = getnode()
info (p) = 10
next (p) = list
list = p
result in which type of operation?
a) pop operation in stack
b) removal of a node
c) inserting a node
d) modifying an existing node

12.A variant of linked list in which last node of the list points to the first
node of the list is?
a) Singly linked list
b) Doubly linked list
c) Circular linked list
d) Multiply linked list

13.What is the functionality of the following code?


public void function(Node node)
{
if(size == 0)
head = node;
else
{
Node temp,cur;
for(cur = head; (temp = cur.getNext())!=null; cur = temp);
cur.setNext(node);
}
size++;
}
a) Inserting a node at the beginning of the list
b) Deleting a node at the beginning of the list
c) Inserting a node at the end of the list
d) Deleting a node at the end of the list

14.Given a singly linked list consisting of N nodes. The task is to remove


duplicates (nodes with duplicate values) from the given list (if exists).
Note: Try not to use extra space. Expected time complexity is O(N). The
nodes are arranged in a sorted way.

Input:
First line of input contains number of testcases T. For each testcase, first
line of input contains length of linked list and next line contains the
linked list data.
Output:
For each testcase, there will be a single line of output which contains
linked list with no duplicates.

User Task:
The task is to complete the function removeDuplicates() which should
remove the duplicates from linked list. The printing is done
automatically by the driver code.
Expected Time Complexity : O(N)
Expected Auxilliary Space : O(1)

Constraints:
1 <= T <= 100
1 <= N <= 104
Example:
Input:
2
4
2245
5
22222
Output:
245
2

Explanation:
Testcase 1: In the given linked list 2 ->2 -> 4-> 5, only 2 occurs more than
1 time.
Testcase 2: In the given linked list 2 ->2 ->2 ->2 ->2, 2 is the only element
and is repeated 5 times.

(OR)
15.Check if Linked List is Palindrome
Given a singly linked list of size N of integers. The task is to check if the
given linked list is palindrome or not.
Expected Time Complexity: O(N)
Expected Auxialliary Space Usage: O(1) (ie, you should not use the
recursive stack space as well)

Input:
First line of input contains number of testcases T. For each testcase, first
line of input contains length of linked list N and next line contains N
integers as data of linked list.

Output:
For each test case output will be 1 if the linked list is a palindrome else 0.

User Task:
The task is to complete the function isPalindrome() which takes head as
reference as the only parameter and returns true or false if linked list is
palindrome or not respectively.

Constraints:
1 <= T <= 103
1 <= N <= 50

Example:
Input:
2
3
121
4
1234
Output:
1
0

Explanation:
Testcase 1: The given linked list is 1 2 1 , which is a pallindrome and
Hence, the output is 1.
Testcase 2: The given linked list is 1 2 3 4 , which is not a pallindrome
and Hence, the output is 0.

You might also like