Most important programming technical interview questions


  1. Reverse a String

python
def reverse_string(s): return s[::-1] # Example reverse_string("hello") # Output: "olleh"
  1. Check if a Number is Prime

python
def is_prime(n): if n <= 1: return False for i in range(2, int(n**0.5) + 1): if n % i == 0: return False return True # Example is_prime(17) # Output: True
  1. Find the Largest Number in a List

python
def find_largest(arr): max_val = arr[0] for num in arr[1:]: if num > max_val: max_val = num return max_val # Example find_largest([1, 5, 3, 9, 2]) # Output: 9
  1. Implement Binary Search

python
def binary_search(arr, target): left, right = 0, len(arr) - 1 while left <= right: mid = (left + right) // 2 if arr[mid] == target: return mid elif arr[mid] < target: left = mid + 1 else: right = mid - 1 return -1 # Example binary_search([1, 2, 3, 4, 5], 3) # Output: 2
  1. Detect a Loop in a Linked List (Floyd’s Cycle Detection)

python
class Node: def __init__(self, val): self.val = val self.next = None 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
  1. Find Middle Element of a Linked List

python
def find_middle(head): slow = fast = head while fast and fast.next: slow = slow.next fast = fast.next.next return slow.val if slow else None
  1. Check if Two Strings are Anagrams

python
def are_anagrams(s1, s2): return sorted(s1) == sorted(s2) # Example are_anagrams("listen", "silent") # Output: True
  1. Merge Two Sorted Linked Lists

python
def merge_two_lists(l1, l2): dummy = current = Node(0) while l1 and l2: if l1.val < l2.val: current.next = l1 l1 = l1.next else: current.next = l2 l2 = l2.next current = current.next current.next = l1 or l2 return dummy.next
  1. Implement Quick Sort

python
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) # Example quick_sort([3,6,8,10,1,2,1]) # Output: [1, 1, 2, 3, 6, 8, 10]
  1. What is a Data Structure? (Short explanation)
    A data structure is a way to store and organize data to enable efficient access and modification. Examples include lists, linked lists, stacks, queues, trees, and graphs.