0% found this document useful (0 votes)
9 views

Practice Questions

Uploaded by

Kamal Raj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Practice Questions

Uploaded by

Kamal Raj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Practice question types for - Tailored for TIE ITCS

DSA- 10M each

Q1. Reverse a Linked List

● Approach: Iterative approach with three pointers.


● Logic:
1. Use three pointers: prev, curr, and next.
2. Initialize prev to null, curr to the head of the list.
3. While curr is not null:
■ Store curr.next in next.
■ Set curr.next to prev.
■ Move prev to curr and curr to next.
4. Return prev as the new head of the reversed list.

Q2. Find the Middle Node of a Linked List

● Approach: Two pointers - slow and fast.


● Logic:
1. Use two pointers, slow and fast, both initialized to the head of the list.
2. Move slowly one step at a time and fast two steps at a time.
3. When fast reaches the end of the list, slow will be pointing to the middle node.

Q3. Check if a String is a Palindrome

● Approach: Two pointers - one at the beginning and one at the end.
● Logic:
1. Use two pointers, left and right, initialized to the beginning and end of the string,
respectively.
2. While left is less than right:
■ If the characters at left and right are not equal, the string is not a palindrome.
■ Otherwise, increment left and decrement right.
3. If the loop completes without finding any unequal characters, the string is a palindrome.

Q4. Find the Maximum Sum Subarray

● Approach: Kadane's Algorithm (Dynamic Programming)


● Logic:
1. Initialize two variables, max_so_far and max_ending_here, both to 0.
2. Iterate through the array.
3. For each element, add it to max_ending_here.
4. If max_ending_here becomes negative, reset it to 0.
5. Update max_so_far with the maximum of max_so_far and max_ending_here.
6. max_so_far will hold the maximum sum of a subarray.
Q5. Binary Search in a Sorted Array

● Approach: Divide and conquer.


● Logic:
1. Set low to 0 and high to the last index of the array.
2. While low is less than or equal to high:
■ Calculate mid as (low + high) / 2.
■ If the element at mid is equal to the target, return mid.
■ If the element at mid is less than the target, set low to mid + 1.
■ If the element at mid is greater than the target, set high to mid - 1.
3. If the loop completes without finding the target, return -1.

Q6. Level Order Traversal of a Binary Tree

● Approach: Breadth-First Search (BFS) using a queue.


● Logic:
1. Enqueue the root node.
2. While the queue is not empty:
■ Dequeue a node.
■ Process the node (e.g., print its value).
■ Enqueue the node's left and right children (if they exist).

Q7. Implement a Queue using Stacks

● Approach: Use two stacks to simulate the behavior of a queue.


● Logic:
1. Use one stack for enqueue operations and the other for dequeue operations.
2. For enqueue, push the element onto the enqueue stack.
3. For dequeue:
■ If the dequeue stack is empty, pop all elements from the enqueue stack and push
them onto the dequeue stack.
■ Pop and return the top element from the dequeue stack.

Q8. Find the Lowest Common Ancestor (LCA) in a Binary Tree

● Approach: Recursive traversal of the tree.


● Logic:
1. If the current node is null or either of the two nodes, return the current node.
2. Recursively find the LCA in the left and right subtrees.
3. If both left and right LCAs are found, return the current node.
4. If only one LCA is found, return that LCA.

Q9. Detect a Cycle in a Linked List

● Approach: Floyd's cycle-finding algorithm (Tortoise and Hare)


● Logic:
1. Use two pointers, slow and fast, both initialized to the head of the list.
2. Move slowly one step at a time and fast two steps at a time.
3. If there is a cycle, slow and fast will eventually meet.
4. If fast reaches the end of the list or becomes null, there is no cycle.

Q10. Merge Sort

● Approach: Divide and conquer.


● Logic:
1. Divide the unsorted list into n sublists, each containing one element (a list of one element
is considered sorted).
2. Repeatedly merge sublists to produce new sorted sublists until there is only one sublist
remaining. This will be the sorted list.

You might also like