1.
Arrays
Definition:
An array is a data structure that stores elements in contiguous memory locations and allows access via indices.
Use Cases:
- Fast access and update of elements
- Representing sequences and matrices
Time Complexities:
- Access: O(1)
- Search: O(n)
- Insert/Delete (end): O(1)
- Insert/Delete (middle): O(n)
Common Operations in Python:
arr = [1, 2, 3, 4, 5]
print(arr[2]) # Accessing
arr.append(6) # Inserting
arr.remove(2) # Deleting
arr.reverse() # Reversing
Practice Questions:
1. Find the second largest number in an array.
2. Rotate an array by k steps.
3. Remove duplicates from a sorted array.
4. Move all zeros to the end of the array.
2. Hashing
Definition:
Hashing involves mapping data to a unique index using a hash function. In Python, dictionaries and sets are
hash-based.
Use Cases:
- Fast lookups
- Counting frequencies
- Detecting duplicates
Time Complexities (average):
- Insert/Search/Delete: O(1)
Common Operations in Python:
arr = [1, 2, 2, 3, 1]
freq = {}
for num in arr:
freq[num] = freq.get(num, 0) + 1
print(freq) # {1: 2, 2: 2, 3: 1}
a = [1, 2, 3]
b = [4, 5, 3]
print(bool(set(a) & set(b))) # True
Practice Questions:
1. Count the number of unique elements in an array.
2. Find the first repeating element.
3. Group words with the same anagram.
3. Two Pointers
Definition:
Two pointers is a technique where two indices move through the data structure simultaneously, often used on sorted
data.
Use Cases:
- Pair problems
- Sorting-based logic
- Efficient traversals
Time Complexity:
- Usually O(n)
Common Operations in Python:
arr = [1, 2, 3, 4, 6]
target = 7
left, right = 0, len(arr) - 1
while left < right:
s = arr[left] + arr[right]
if s == target:
print(True)
break
elif s < target:
left += 1
else:
right -= 1
Practice Questions:
1. Find a pair with a given sum in a sorted array.
2. Remove duplicates from a sorted array in-place.
3. Merge two sorted arrays.
4. Sliding Window
Definition:
A technique for problems involving subarrays or substrings where a fixed or variable window "slides" over the data.
Use Cases:
- Maximum/minimum sum
- Longest/shortest substring
- Efficient range-based checks
Time Complexity:
- Typically O(n)
Common Operations in Python:
arr = [2, 1, 5, 1, 3, 2]
k=3
max_sum = sum(arr[:k])
window_sum = max_sum
for i in range(k, len(arr)):
window_sum += arr[i] - arr[i-k]
max_sum = max(max_sum, window_sum)
print(max_sum) # 9
Practice Questions:
1. Longest substring without repeating characters.
2. Max sum subarray of size k.
3. Min window substring with all characters.
5. Stacks
Definition:
A stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle.
Use Cases:
- Undo operations
- Expression evaluation
- Balancing symbols
Time Complexities:
- Push/Pop/Peek: O(1)
Common Operations in Python:
s = "hello"
stack = list(s)
reversed_str = ""
while stack:
reversed_str += stack.pop()
print(reversed_str) # "olleh"
def is_balanced(s):
stack = []
mapping = {')': '(', ']': '[', '}': '{'}
for char in s:
if char in mapping.values():
stack.append(char)
elif char in mapping:
if not stack or stack[-1] != mapping[char]:
return False
stack.pop()
return not stack
print(is_balanced("({[]})")) # True
Practice Questions:
1. Reverse a string using a stack.
2. Check for balanced parentheses.
3. Evaluate a postfix expression.