✅ Top Coding Algorithms You MUST Know for Interviews 💼👨💻
🟢 1. Bubble Sort – Sorting Algorithm
👉 Repeatedly compares & swaps adjacent elements to sort the array.
*Python*
👉 Efficiently searches a sorted array in O(log n) time.
*Python*
👉 Function calls itself to solve smaller subproblems.
*C++*
👉 Stores previous results to avoid repeated work.
*Python*
👉 Finds max sum in a subarray of fixed length in O(n) time.
*Java*
👉 Explores all neighbors before going deeper.
*Python*
Note: I've added
🟢 1. Bubble Sort – Sorting Algorithm
👉 Repeatedly compares & swaps adjacent elements to sort the array.
*Python*
def bubble_sort(arr):*C++*
for i in range(len(arr)):
for j in range(len(arr)-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
void bubbleSort(int arr[], int n) {*Java*
for(int i=0; i<n-1; i++)
for(int j=0; j<n-i-1; j++)
if(arr[j] > arr[j+1])
swap(arr[j], arr[j+1]);
}
void bubbleSort(int[] arr) {🟡 2. Binary Search – Searching Algorithm
for(int i = 0; i < arr.length - 1; i++)
for(int j = 0; j < arr.length - i - 1; j++)
if(arr[j] > arr[j+1]) {
int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp;
}
}
👉 Efficiently searches a sorted array in O(log n) time.
*Python*
def binary_search(arr, target):🟠 3. Recursion – Factorial Example
low, high = 0, len(arr)-1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target: return mid
elif arr[mid] < target: low = mid + 1
else: high = mid - 1
return -1
👉 Function calls itself to solve smaller subproblems.
*C++*
int factorial(int n) {🔵 4. Dynamic Programming – Fibonacci (Bottom-Up)
if(n == 0) return 1;
return n * factorial(n - 1);
}
👉 Stores previous results to avoid repeated work.
*Python*
def fib(n):🟣 5. Sliding Window – Max Sum Subarray of Size K
dp = [0, 1]
for i in range(2, n+1):
dp.append(dp[i-1] + dp[i-2])
return dp[n]
👉 Finds max sum in a subarray of fixed length in O(n) time.
*Java*
int maxSum(int[] arr, int k) {🧠 6. BFS (Breadth-First Search) – Graph Traversal
int sum = 0, max = 0;
for(int i = 0; i < k; i++) sum += arr[i];
max = sum;
for(int i = k; i < arr.length; i++) {
sum += arr[i] - arr[i - k];
if(sum > max) max = sum;
}
return max;
}
👉 Explores all neighbors before going deeper.
*Python*
from collections import deque👍 Tap ❤️ for more! #coding #algorithms #interviews #programming #datastructures
def bfs(graph, start):
visited = set([start])
queue = deque([start])
while queue:
node = queue.popleft()
print(node)
for neighbor in graph[node]:
if neighbor not in visited:
visited.add(neighbor)
queue.append(neighbor)
Note: I've added
around code snippets to format them correctly in Telegram.
❤11👏1