Programming Questions
1. Maximum Subarray Sum (Kadane’s Algorithm)
Problem: Find the contiguous subarray that has the largest sum.
Test Case: Input [-2,1,-3,4,-1,2,1,-5,4], Output 6.
2. Trapping Rainwater Problem
Problem: Given an array of heights, find how much water can be trapped.
Test Case: Input [3,0,2,0,4], Output 7.
3. Rotate Array In-Place
Problem: Rotate an array k steps without using extra space.
Test Case: Input [1,2,3,4,5,6,7], k=3 → Output [5,6,7,1,2,3,4].
4. Find Duplicate without Extra Space
Problem: Array of n+1 with values 1..n. Find duplicate in O(1) space.
Test Case: Input [3,1,3,4,2], Output 3.
5. Subarray with Given Sum
Problem: Find if a subarray with given sum S exists.
Test Case: Input [1,4,20,3,10,5], S=33 → True.
6. Next Greater Element
Problem: For each element, find the next greater element.
Test Case: Input [4,5,2,25], Output [5,25,25,-1].
7. Equilibrium Index
Problem: Find index where left sum = right sum.
Test Case: Input [-7,1,5,2,-4,3,0], Output index 3.
8. Longest Consecutive Sequence
Problem: Find length of longest consecutive elements sequence.
Test Case: Input [100,4,200,1,3,2], Output 4.
9. Pair with Given Difference
Problem: Find if pair with difference k exists.
Test Case: Input [5,20,3,2,50,80], k=78 → True.
10. Minimum Swaps to Sort
Problem: Find minimum swaps required to sort.
Test Case: Input [4,3,2,1], Output 2.
11. Maximum Product Subarray
Problem: Find contiguous subarray with largest product.
Test Case: Input [2,3,-2,4], Output 6.
12. Count Inversions in Array
Problem: Count pairs (i,j) where i<j and arr[i]>arr[j].
Test Case: Input [2,4,1,3,5], Output 3.
13. Majority Element (Boyer-Moore)
Problem: Find element occurring more than n/2 times.
Test Case: Input [3,3,4,2,3,3,5,3], Output 3.
14. Stock Buy and Sell – Max Profit
Problem: Find max profit from single buy-sell.
Test Case: Input [7,1,5,3,6,4], Output 5.
15. Maximum Circular Subarray Sum
Problem: Find max subarray sum in circular array.
Test Case: Input [5,-2,3,4], Output 12.
16. Find Median of Two Sorted Arrays
Problem: Find median of two sorted arrays.
Test Case: Input [1,3] & [2], Output 2.0.
17. Search in Rotated Sorted Array
Problem: Search element in rotated sorted array.
Test Case: Input [4,5,6,7,0,1,2], target=0 → index 4.
18. Find All Subarrays with 0 Sum
Problem: Print all subarrays with sum 0.
Test Case: Input [6,3,-1,-3,4,-2,2,4,6,-12,-7], multiple outputs.
19. Maximum Length Subarray with Equal 0s and 1s
Problem: Find longest subarray with equal 0s and 1s.
Test Case: Input [0,1,0,1,1,1,0], Output 6.
20. Three Sum Problem
Problem: Find all unique triplets with sum=0.
Test Case: Input [-1,0,1,2,-1,-4], Output [[-1,-1,2], [-1,0,1]].