🚀 Coding Interview Questions with Answers — Part 2
🌱 Arrays, Strings & Two-Pointers
🚀 11. How do you remove duplicates from a sorted array?
Since the array is already sorted, duplicates appear together.
🔹 Best Approach
Use the Two-Pointer Technique.
- One pointer tracks unique elements
- Another scans the array
🔹 Python Solution
def remove_duplicates(arr):
if not arr:
return 0
i = 0
for j in range(1, len(arr)):
if arr[j]!= arr[i]:
i += 1
arr[i] = arr[j]
return i + 1
arr = [1,1,2,2,3,4,4]
length = remove_duplicates(arr)
print(arr[:length])
🔹 Output
[1][2][3][4]
🔹 Complexity
Time → O(n)
Space → O(1)
🔹 Interview Tip
This is one of the most common two-pointer interview problems.
🚀 12. How do you solve “Two Sum” efficiently?
Problem: Find two numbers whose sum equals target.
🔹 Brute Force
for i in range(len(arr)):
for j in range(i+1, len(arr)):
if arr[i] + arr[j] == target:
return [i, j]
Complexity → O(n²)
🔹 Optimized HashMap Solution
def two_sum(arr, target):
hashmap = {}
for i, num in enumerate(arr):
complement = target - num
if complement in hashmap:
return [hashmap[complement], i]
hashmap[num] = i
print(two_sum([2,7,11,15], 9))
🔹 Output
[0][1]
🔹 Complexity
Time → O(n)
Space → O(n)
🔹 Interview Tip
Hashing is the key optimization here.
🚀 13. How do you reverse a string or array?
🔹 Reverse String
s = "hello"
print(s[::-1])
Output → olleh
🔹 Two-Pointer Method
def reverse_array(arr):
left = 0
right = len(arr) - 1
while left < right:
arr[left], arr[right] = arr[right], arr[left]
left += 1
right -= 1
return arr
print(reverse_array([1,2,3,4]))
🔹 Complexity
Time → O(n)
Space → O(1)
🔹 Interview Tip
Interviewers often prefer the two-pointer approach.
🚀 14. How do you find the maximum subarray sum (Kadane’s Algorithm)?
Problem: Find contiguous subarray with maximum sum.
🔹 Kadane’s Algorithm
def max_subarray(arr):
current_sum = arr[0]
max_sum = arr[0]
for num in arr[1:]:
current_sum = max(num, current_sum + num)
max_sum = max(max_sum, current_sum)
return max_sum
print(max_subarray([-2,1,-3,4,-1,2,1,-5,4]))
🔹 Output
6
Subarray:
[4, -1, 2, 1]
🔹 Complexity
Time → O(n)
Space → O(1)
🔹 Interview Tip
Kadane’s Algorithm is a very high-frequency interview question.
🚀 15. How do you rotate an array?
Rotate array by k positions.
🔹 Python Solution
def rotate(arr, k):
k = k % len(arr)
return arr[-k:] + arr[:-k]
print(rotate([1,2,3,4,5], 2))
🔹 Output
[4][5][1][2][3]
🔹 Complexity
Time → O(n)
Space → O(n)
🔹 In-Place Optimization
Can be solved in O(1) extra space using reversal algorithm.
🚀 16. How do you find the first missing positive number?
Problem: Find smallest missing positive integer.
Example: [3,4,-1,1]
Output: 2
🔹 Optimized Solution Idea
Place each number at its correct index.
1 → index 0
2 → index 1
🔹 Python Solution
def first_missing_positive(nums):
n = len(nums)
for i in range(n):
while 1 <= nums[i] <= n and nums[nums[i]-1]!= nums[i]:
nums[nums[i]-1], nums[i] = nums[i], nums[nums[i]-1]
for i in range(n):
if nums[i]!= i + 1:
return i + 1
return n + 1
print(first_missing_positive([3,4,-1,1]))
🔹 Complexity
Time → O(n)
Space → O(1)
🔹 Interview Tip
This is considered a hard interview problem.
🚀 17. How do you implement sliding-window problems?
Sliding window helps optimize subarray/substring problems.
🔹 Example Problem
Maximum sum of subarray of size k.
def max_sum(arr, k):
window_sum = sum(arr[:k])
max_sum = window_sum
for i in range(k, len(arr)):
window_sum += arr[i] - arr[i-k]
max_sum = max(max_sum, window_sum)
return max_sum
print(max_sum([1,2,3,4,5], 3))
🔹 Output
12