Prepare Interview

Mock Exams

Make Homepage

## Experienced / Expert level questions & answers

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