Array DSA solutions Python
Got it ✅ — You want all important Array DSA questions for Data Engineering
interviews with Python shortcut code + simple explanation + Time & Space
Complexity.
Here’s your condensed cheat sheet for quick revision before the interview.
1. Reverse an Array
python
def reverse_array(arr):
return arr[::-1] # Python slicing
Explanation: Slicing with [::-1] reverses the array.
Time: O(n) | Space: O(n) (O(1) if done in-place with swapping).
2. Find Max and Min in Array
python
def min_max(arr):
return min(arr), max(arr) # Built-in functions
Explanation: Finds smallest and largest in one pass.
Time: O(n) | Space: O(1).
3. Find Second Largest Element
python
def second_largest(arr):
uniq = list(set(arr))
1/9
uniq.sort(reverse=True)
return uniq[1] if len(uniq) > 1 else None
Explanation: Remove duplicates → sort → pick 2nd.
Time: O(n log n) | Space: O(n).
(Better O(n) version exists with one pass.)
4. Check if Array is Sorted
python
def is_sorted(arr):
return all(arr[i] <= arr[i+1] for i in range(len(arr)-1))
Time: O(n) | Space: O(1).
5. Rotate Array by K
python
def rotate(arr, k):
k %= len(arr)
return arr[-k:] + arr[:-k]
Time: O(n) | Space: O(n) (O(1) with in-place reversal).
6. Remove Duplicates (Sorted Array)
python
def remove_duplicates(arr):
res = []
for num in arr:
if not res or res[-1] != num:
2/9
res.append(num)
return res
Time: O(n) | Space: O(n).
7. Find Missing Number (1 to n)
python
def missing_number(arr):
n = len(arr) + 1
return n*(n+1)//2 - sum(arr)
Time: O(n) | Space: O(1).
8. Find All Pairs with Given Sum
python
def two_sum(arr, target):
seen = {}
for num in arr:
if target - num in seen:
return (target - num, num)
seen[num] = True
Time: O(n) | Space: O(n).
9. Kadane’s Algorithm (Max Subarray Sum)
python
def max_subarray_sum(arr):
max_sum = cur_sum = arr[0]
for num in arr[1:]:
3/9
cur_sum = max(num, cur_sum + num)
max_sum = max(max_sum, cur_sum)
return max_sum
Time: O(n) | Space: O(1).
10. Merge Two Sorted Arrays
python
def merge_sorted(a, b):
i=j=0
res = []
while i < len(a) and j < len(b):
if a[i] < b[j]:
res.append(a[i]); i += 1
else:
res.append(b[j]); j += 1
res.extend(a[i:]); res.extend(b[j:])
return res
Time: O(n+m) | Space: O(n+m).
11. Move Zeroes to End
python
def move_zeroes(arr):
pos = 0
for i in range(len(arr)):
if arr[i] != 0:
arr[pos], arr[i] = arr[i], arr[pos]
pos += 1
return arr
Time: O(n) | Space: O(1).
4/9
12. Find Intersection of Two Arrays
python
def intersection(a, b):
return list(set(a) & set(b))
Time: O(n+m) | Space: O(min(n, m)).
If you want, I can prepare you a one-page PDF “DSA Arrays Quick Revision Sheet”
with all patterns, formulas, and tricky variations so you can revise in 5 minutes
before your interview.
Do you want me to make that PDF now?
Got it ✅ — You want all important String DSA questions for a Data Engineering
interview in Python shortcut form, with simple explanations and time & space
complexity.
Here’s your String DSA cheat sheet for quick revision.
1. Reverse a String
python
def reverse_string(s):
return s[::-1]
Explanation: Python slicing with step -1 reverses.
Time: O(n) | Space: O(n) (O(1) if in-place for char array).
5/9
2. Check Palindrome
python
def is_palindrome(s):
return s == s[::-1]
Explanation: Compare string to its reverse.
Time: O(n) | Space: O(n).
3. Count Vowels and Consonants
python
def count_vowels(s):
vowels = "aeiouAEIOU"
v = sum(1 for ch in s if ch in vowels)
return v, len(s) - v
Time: O(n) | Space: O(1).
4. First Non-Repeating Character
python
from collections import Counter
def first_non_repeating(s):
count = Counter(s)
for ch in s:
if count[ch] == 1:
return ch
Time: O(n) | Space: O(n).
6/9
5. Anagram Check
python
def is_anagram(s1, s2):
return sorted(s1) == sorted(s2)
Time: O(n log n) | Space: O(n).
(Better: count characters → O(n) time.)
6. Remove All Duplicates
python
def remove_duplicates(s):
return "".join(sorted(set(s), key=s.index))
Time: O(n) | Space: O(n).
7. Check if Rotation of Another
python
def is_rotation(s1, s2):
return len(s1) == len(s2) and s2 in (s1 + s1)
Time: O(n) | Space: O(n).
8. Longest Common Prefix
python
def longest_common_prefix(strs):
prefix = strs[0]
for s in strs[1:]:
7/9
while not s.startswith(prefix):
prefix = prefix[:-1]
return prefix
Time: O(n * m) | Space: O(1).
9. Find All Substrings
python
def all_substrings(s):
return [s[i:j] for i in range(len(s)) for j in range(i+1, len(s)+1)]
Time: O(n²) | Space: O(n²).
10. String Compression (Run Length Encoding)
python
def compress(s):
res = ""
count = 1
for i in range(1, len(s)+1):
if i < len(s) and s[i] == s[i-1]:
count += 1
else:
res += s[i-1] + str(count)
count = 1
return res
Time: O(n) | Space: O(n).
11. Longest Palindromic Substring (Expand Around
Center)
8/9
python
def longest_palindrome(s):
res = ""
for i in range(len(s)):
for a, b in [(i, i), (i, i+1)]:
while a >= 0 and b < len(s) and s[a] == s[b]:
if (b-a+1) > len(res):
res = s[a:b+1]
a -= 1; b += 1
return res
Time: O(n²) | Space: O(1).
12. Check Pangram
python
def is_pangram(s):
return set('abcdefghijklmnopqrstuvwxyz') <= set(s.lower())
Time: O(n) | Space: O(1).
If you want, I can now make you a “One-Page DSA String Quick Sheet” with all
patterns, tricky variations, and best approaches so you can revise all string
problems in 5 minutes before your interview.
Do you want me to prepare that PDF for you?
Hey! Looks like you sent just a dot — anything specific you want me to help with?
More string DSA questions? Or maybe array ones? Or something else for your data
engineering interview?
9/9