Prepare Interview

Mock Exams

Make Homepage

## Freshers / Beginner level questions & answers

### Ques 1. Find the kth to last element of a singly linked list.

Use two pointers; one moves k nodes into the list, and then both iterate until the second one reaches the end.

Example:

`Input: 1 -> 2 -> 3 -> 4 -> 5, k = 2Output: 4`

### Ques 2. Remove duplicates from an unsorted linked list.

Use a hash set to keep track of unique elements while traversing the list.

Example:

`Input: 1 -> 2 -> 2 -> 3 -> 4 -> 4 -> 5Output: 1 -> 2 -> 3 -> 4 -> 5`

### Ques 3. Write a function to find the middle element of a linked list.

Use two pointers; one moves one step at a time, and the other moves two steps at a time.

Example:

`Input: 1 -> 2 -> 3 -> 4 -> 5Output: 3`

## Intermediate / 1 to 5 years experienced level questions & answers

### Ques 4. Implement a function to reverse a singly linked list.

To reverse a linked list, you can iterate through the list and reverse the direction of pointers.

Example:

`Input: 1 -> 2 -> 3 -> 4 -> 5Output: 5 -> 4 -> 3 -> 2 -> 1`

### Ques 5. Detect a cycle in a linked list.

You can use Floyd's cycle-finding algorithm, also known as the 'tortoise and hare' algorithm.

Example:

`Input: 1 -> 2 -> 3 -> 4 -> 5 -> 2 (cycle)Output: True`

### Ques 6. Merge two sorted linked lists into a single sorted list.

Traverse both lists simultaneously, compare elements, and merge them into a new sorted list.

Example:

`Input: List1: 1 -> 3 -> 5, List2: 2 -> 4 -> 6Output: 1 -> 2 -> 3 -> 4 -> 5 -> 6`

### Ques 7. Check if a linked list is a palindrome.

Reverse the second half of the list and compare it with the first half.

Example:

`Input: 1 -> 2 -> 3 -> 2 -> 1Output: True`

### Ques 8. Swap nodes in pairs in a linked list.

Iterate through the list and swap each pair of adjacent nodes.

Example:

`Input: 1 -> 2 -> 3 -> 4Output: 2 -> 1 -> 4 -> 3`

### Ques 9. Rotate a linked list by k places.

Find the length of the list, then move (length - k % length) steps to the right.

Example:

`Input: 1 -> 2 -> 3 -> 4 -> 5, k = 2Output: 4 -> 5 -> 1 -> 2 -> 3`

### Ques 10. Detect the intersection point of two linked lists.

Find the lengths of both lists, move the longer list's pointer ahead, and then iterate to find the intersection.

Example:

`Input: List1: 1 -> 2 -> 3, List2: 6 -> 5 -> 2 -> 3Output: Node with value 2`

## Experienced / Expert level questions & answers

### Ques 11. Implement a function to add two numbers represented by linked lists.

Traverse both lists simultaneously, perform addition, and handle carry.

Example:

`Input: (7 -> 2 -> 4) + (5 -> 6 -> 4)Output: 2 -> 9 -> 8`

### Ques 12. Clone a linked list with next and random pointers.

Create a copy of each node and link the copies, then separate the original and cloned lists.

Example:

`Input: 1 -> 2 -> 3 -> 4Output: 1' -> 2' -> 3' -> 4'`

### Ques 13. Flatten a multilevel doubly linked list.

Use recursion to flatten each level and connect the flattened levels.

Example:

`Input: 1 <-> 2 <-> 3 <-> 7 <-> 8 <-> 11 <-> 4 <-> 9 <-> 12Output: 1 <-> 2 <-> 3 <-> 4 <-> 7 <-> 8 <-> 9 <-> 11 <-> 12`

### Ques 14. Implement LRU (Least Recently Used) Cache using a linked list.

Maintain a doubly linked list to represent usage order and a hash map for quick access.

Example:

`Cache capacity = 3Operations: Get(2), Put(2, 6), Get(1), Put(1, 5), Put(1, 2)Output: 6, 5, 2`

### Ques 15. Reverse nodes in k-group in a linked list.

Reverse k nodes at a time, and connect the reversed groups.

Example:

`Input: 1 -> 2 -> 3 -> 4 -> 5, k = 2Output: 2 -> 1 -> 4 -> 3 -> 5`