Prepare Interview

Mock Exams

Make Homepage

The Best LIVE Mock Interview - You should go through before Interview

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

### Ques 1. 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 2. 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 3. 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 4. 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 5. 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 6. 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 7. 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`