40 Most Asked DSA Questions to Clear Your Next Interview

Listen to this Post

Featured Image
Here’s a comprehensive list of 40 Data Structures and Algorithms (DSA) questions frequently asked in technical interviews, along with practical implementations in Python, C++, and Linux commands where applicable.

1. Reverse a Linked List

class Node: 
def <strong>init</strong>(self, data): 
self.data = data 
self.next = None

def reverse_linked_list(head): 
prev = None 
current = head 
while current: 
next_node = current.next 
current.next = prev 
prev = current 
current = next_node 
return prev 
  1. Find the Middle Element of a Linked List
    def find_middle(head): 
    slow = fast = head 
    while fast and fast.next: 
    slow = slow.next 
    fast = fast.next.next 
    return slow.data 
    

3. Implement a Stack Using Arrays

class Stack: 
def <strong>init</strong>(self): 
self.stack = []

def push(self, item): 
self.stack.append(item)

def pop(self): 
return self.stack.pop() if self.stack else None 

4. Implement a Queue Using Linked Lists

class QueueNode: 
def <strong>init</strong>(self, data): 
self.data = data 
self.next = None

class Queue: 
def <strong>init</strong>(self): 
self.front = self.rear = None

def enqueue(self, item): 
new_node = QueueNode(item) 
if not self.rear: 
self.front = self.rear = new_node 
return 
self.rear.next = new_node 
self.rear = new_node

def dequeue(self): 
if not self.front: 
return None 
temp = self.front 
self.front = temp.next 
if not self.front: 
self.rear = None 
return temp.data 

5. Find Factorial Using Recursion

def factorial(n): 
return 1 if n == 0 else n  factorial(n - 1) 

6. Binary Search in an Array

def binary_search(arr, target): 
low, high = 0, len(arr) - 1 
while low <= high: 
mid = (low + high) // 2 
if arr[bash] == target: 
return mid 
elif arr[bash] < target: 
low = mid + 1 
else: 
high = mid - 1 
return -1 

7. Find Largest/Smallest Element in an Array

 Linux command to find largest number in a file 
awk 'BEGIN {max = 0} {if ($1 > max) max = $1} END {print max}' numbers.txt 

8. Merge Sort Implementation

def merge_sort(arr): 
if len(arr) > 1: 
mid = len(arr) // 2 
left = arr[:mid] 
right = arr[mid:] 
merge_sort(left) 
merge_sort(right) 
i = j = k = 0 
while i < len(left) and j < len(right): 
if left[bash] < right[bash]: 
arr[bash] = left[bash] 
i += 1 
else: 
arr[bash] = right[bash] 
j += 1 
k += 1 
while i < len(left): 
arr[bash] = left[bash] 
i += 1 
k += 1 
while j < len(right): 
arr[bash] = right[bash] 
j += 1 
k += 1 

9. Quick Sort Implementation

def quick_sort(arr): 
if len(arr) <= 1: 
return arr 
pivot = arr[len(arr) // 2] 
left = [x for x in arr if x < pivot] 
middle = [x for x in arr if x == pivot] 
right = [x for x in arr if x > pivot] 
return quick_sort(left) + middle + quick_sort(right) 
  1. Detect a Cycle in a Linked List (Floyd’s Algorithm)
    def has_cycle(head): 
    slow = fast = head 
    while fast and fast.next: 
    slow = slow.next 
    fast = fast.next.next 
    if slow == fast: 
    return True 
    return False 
    

You Should Know: