Day 22

Tricky String & Array Problems

Welcome to Day 22 of the Python Coding Interview Challenge!
Master the most commonly asked string and array problems that test your problem-solving skills in technical interviews.

📌 Interview Questions & Solutions

1

Reverse a String without using built-in functions

def reverse_string(s): return s[::-1] print(reverse_string("ShaiviConnect")) # tcennoCiviahS
2

Check if a String is a Palindrome

def is_palindrome(s): return s == s[::-1] print(is_palindrome("madam")) # True
3

Find the First Non-Repeating Character

from collections import Counter def first_non_repeating(s): count = Counter(s) for char in s: if count[char] == 1: return char return None print(first_non_repeating("aabbcde")) # c
4

Check if Two Strings are Anagrams

def are_anagrams(s1, s2): return sorted(s1) == sorted(s2) print(are_anagrams("listen", "silent")) # True
5

Find Missing Number in an Array

def missing_number(arr, n): total = n * (n + 1) // 2 return total - sum(arr) print(missing_number([1, 2, 4, 5], 5)) # 3
6

Find Duplicate in an Array

def find_duplicate(arr): seen = set() for num in arr: if num in seen: return num seen.add(num) return None print(find_duplicate([1, 2, 3, 4, 2])) # 2
7

Rotate an Array by K steps

def rotate_array(arr, k): k = k % len(arr) return arr[-k:] + arr[:-k] print(rotate_array([1, 2, 3, 4, 5], 2)) # [4, 5, 1, 2, 3]
8

Find Maximum Subarray Sum (Kadane's Algorithm)

def max_subarray_sum(arr): max_current = max_global = arr[0] for num in arr[1:]: max_current = max(num, max_current + num) if max_current > max_global: max_global = max_current return max_global print(max_subarray_sum([-2,1,-3,4,-1,2,1,-5,4])) # 6

✅ Key Takeaways

💡
Mastering tricky problems builds strong problem-solving ability and algorithmic thinking.
âš¡
Always optimize your solutions - Kadane's Algorithm achieves O(n) time complexity.
🎯
String & Array questions are fundamental and appear in almost every technical interview.