TGViewer
Coding Interview Resources Coding Interview Resources @crackingthecodinginterview ยท 52.2K subscribers
Post #2954 1.02K
๐Ÿš€ 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

๐Ÿ”น Complexity
Time โ†’ O(n)
Space โ†’ O(1)

๐Ÿ”น Interview Tip
Sliding window is heavily used in:
- Substrings
- Subarrays
- Streaming data
  • โค 2
More from @crackingthecodinginterview
  1. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  2. Oct 7, 2026๐Ÿš€ DSA Topics Every Programmer Should Know ๐Ÿ’ป๐Ÿ”ฅ ๐Ÿ“ฆ 1. Arrays โœ” Traversal โœ” Searching โœ” Sorโ€ฆ
  3. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  4. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  5. Sep 29, 2026โœ… Daily Coding Habits That Make You a Better Developer ๐Ÿง ๐Ÿ’ปโœจ 1๏ธโƒฃ Code Every Day (Even 30 Mโ€ฆ
  6. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’