Coding Interview Questions

  1. Two Sum Problem: Given an array of integers, return indices of the two numbers such that they add up to a specific target.
    • Solution: Use a hashmap to store the complement of each element.
  2. Reverse a String: Write a function that reverses a string.
    • Solution: Iterate through the string and swap characters from start to end.
  3. Palindrome Checker: Determine if a string is a palindrome.
    • Solution: Compare characters from start and end until the middle of the string.
  4. FizzBuzz: Print numbers from 1 to n, but for multiples of 3 print “Fizz,” for multiples of 5 print “Buzz,” and for multiples of both 3 and 5 print “FizzBuzz.”
    • Solution: Use modulo operations to check divisibility.
  5. Find the Maximum Subarray: Given an array, find the contiguous subarray with the largest sum.
    • Solution: Use Kadane’s algorithm to find the maximum sum subarray.
  6. Merge Sorted Arrays: Merge two sorted arrays into a single sorted array.
    • Solution: Use two pointers or indices to compare elements and merge the arrays.
  7. Linked List Cycle Detection: Determine if a linked list has a cycle.
    • Solution: Use fast and slow pointers to detect a cycle in the linked list.
  8. Binary Search: Implement binary search for a sorted array.
    • Solution: Compare the target value with the middle element and narrow down the search space.
  9. Counting Characters in a String: Count occurrences of each character in a string.
    • Solution: Use a hashmap to store character counts.
  10. Validate Parentheses: Given a string containing just parentheses, determine if the parentheses are valid.
    • Solution: Use a stack to keep track of opening and closing parentheses.
  11. Reverse a Linked List: Reverse a singly linked list.
    • Solution: Use iterative or recursive approaches to reverse the links between nodes.
  12. Check for Anagrams: Check if two strings are anagrams of each other.
    • Solution: Sort both strings and compare or use character frequency counting.
  13. Implement a Queue using Stacks: Implement a queue data structure using stacks.
    • Solution: Maintain two stacks to simulate queue operations.
  14. Rotate Array: Rotate an array to the right by k steps.
    • Solution: Use reverse techniques or create a new array with the rotated elements.
  15. Find Missing Number: Given an array containing n distinct numbers taken from 0 to n, find the missing number.
    • Solution: Use summation formula or XOR operations to find the missing number.
  16. String Reversal without Special Characters: Reverse a string without affecting special characters.
    • Solution: Use two pointers to reverse only alphanumeric characters.
  17. Find Peak Element: Find any peak element in an array.
    • Solution: Use binary search to find a peak element.
  18. Implement a Stack using Queues: Implement a stack data structure using queues.
    • Solution: Maintain two queues to simulate stack operations.
  19. String Compression: Compress a string by counting repeated characters.
    • Solution: Iterate through the string and count consecutive characters.
  20. Spiral Matrix: Print a matrix in spiral order.
    • Solution: Traverse the matrix in layers, going from outer to inner elements.
  21. Longest Common Prefix: Find the longest common prefix string amongst an array of strings.
    • Solution: Compare characters of strings from the beginning until the prefix ends.
  22. Intersection of Two Arrays: Find the intersection of two arrays.
    • Solution: Use hash sets or sorting and two pointers to find common elements.
  23. Subsets: Generate all possible subsets of a set.
    • Solution: Use recursion to generate subsets by including or excluding elements.
  24. Valid Sudoku: Determine if a 9×9 Sudoku board is valid.
    • Solution: Validate rows, columns, and subgrids for duplicate numbers.
  25. Product of Array Except Self: Given an array, calculate the product of every element except itself.
    • Solution: Use prefix and suffix product arrays or optimize space using two passes.
  26. Merge Intervals: Merge overlapping intervals.
    • Solution: Sort intervals and merge overlapping ones by comparing start and end times.
  27. Find First and Last Position of Element in Sorted Array: Find the starting and ending position of a given target value in a sorted array.
    • Solution: Use binary search to find the first and last occurrences of the target.
  28. Minimum Size Subarray Sum: Find the minimum size subarray sum that equals or exceeds a given value.
    • Solution: Use a two-pointer approach to track the subarray.
  29. Evaluate Reverse Polish Notation: Evaluate the value of an arithmetic expression in Reverse Polish Notation.
    • Solution: Use a stack to perform operations based on the postfix expression.
  30. Valid Palindrome II: Given a non-empty string, check if it can be made a palindrome by removing at most one character.
    • Solution: Use two pointers to check for symmetry while allowing one character deletion.
  31. Meeting Rooms II: Given an array of meeting time intervals, find the minimum number of conference rooms required.
    • Solution: Sort intervals based on start times and use a priority queue to track room allocations.
  32. Kth Largest Element in an Array: Find the kth largest element in an unsorted array.
    • Solution: Use a priority queue or quick select algorithm to find the kth largest element.
  33. Longest Substring Without Repeating Characters: Find the length of the longest substring without repeating characters.
    • Solution: Use a sliding window approach to track the longest substring without duplicates.
  34. Top K Frequent Elements: Given an integer array, return the k most frequent elements.
    • Solution: Use hash maps for frequency counting and a priority queue or sorting to find the top k elements.
  35. Maximum Depth of Binary Tree: Find the maximum depth of a binary tree.
    • Solution: Use recursion to traverse the tree and calculate the maximum depth.

Leave a Comment