LC-TEST-20 — Algorithm Test (V4)
🌍 Why This Document Exists
This is not a LeetCode solution dump.
This is a real-world algorithm thinking gym.
You will train:
- 🧠 Problem decomposition
- 📊 Data processing at scale
- ⏱ Time & space complexity awareness
- 🔁 Iteration, recursion, sliding windows
- 🧮 Hash maps, sets, heaps, stacks
- 🧵 Real production-like constraints
Inspired by Google, Microsoft, OpenAI interviews.
🏆 Python Logic — Real World Edition (20 Problems)
Difficulty increases gradually.
⚠️ Do not open solutions too early.
🟢 Problem 1 — Manual Sum (Warm-Up)
logs = [12, 0, 7, 3, 9, 0, 15]
Task:
Sum all values without using sum()
✅ Solution — Pure Logic
total = 0
for x in logs:
total += x
print(total)
Time: O(n) Space: O(1)
🟢 Problem 2 — Boolean Health Check
heartbeat = [True, True, False, True]
Healthy only if no False exists
✅ Solution — Early Exit
healthy = True
for h in heartbeat:
if not h:
healthy = False
break
print(healthy)
Time: O(n) Space: O(1)
🟢 Problem 3 — Unique User Count
users = [101, 203, 101, 405, 203]
✅ Solution 1 — Set
print(len(set(users)))
Time: O(n) Space: O(n)
✅ Solution 2 — NO LIB (Pure)
seen = {}
count = 0
for u in users:
if u not in seen:
seen[u] = True
count += 1
print(count)
🟡 Problem 4 — First Duplicate Event
events = ["A","B","C","B","D"]
✅ Solution — Hash Tracking
seen = set()
for e in events:
if e in seen:
print(e)
break
seen.add(e)
Time: O(n) Space: O(n)
🟡 Problem 5 — Fixed Sliding Window Alert
cpu = [40,60,80,90,30]
Alert if any 3 consecutive sum > 200
✅ Solution — Sliding Window
window = cpu[0] + cpu[1] + cpu[2]
for i in range(3, len(cpu)):
if window > 200:
print(True)
break
window += cpu[i] - cpu[i-3]
else:
print(window > 200)
Time: O(n) Space: O(1)
🟡 Problem 6 — Rate Limiter (Multi-Solution)
(same as your version — unchanged)
🟡 Problem 7 — Most Frequent Element
errors = [500,404,500,403,404,500]
✅ Solution 1 — Dict
freq = {}
for e in errors:
freq[e] = freq.get(e, 0) + 1
best = None
best_count = 0
for k,v in freq.items():
if v > best_count:
best, best_count = k, v
print(best)
🔵 Problem 8 — Manual Chunking
data = [0,1,2,3,4,5,6]
size = 3
✅ Solution — No Lib
res = []
i = 0
while i < len(data):
batch = []
for j in range(size):
if i+j < len(data):
batch.append(data[i+j])
res.append(batch)
i += size
print(res)
Time: O(n) Space: O(n)
🔵 Problem 9 — Valid Parentheses
s = "{[()()]}"
✅ Solution — Stack
stack = []
pairs = {')':'(', ']':'[', '}':'{'}
for c in s:
if c in pairs.values():
stack.append(c)
else:
if not stack or stack.pop() != pairs[c]:
print(False)
break
else:
print(len(stack) == 0)
🔵 Problem 10 — Longest 1s Streak
status = [1,1,0,1,1,1]
✅ Solution
cur = best = 0
for s in status:
if s == 1:
cur += 1
best = max(best, cur)
else:
cur = 0
print(best)
🔵 Problem 11 — Merge Sorted Lists (NO SORT)
a = [1,3,5]
b = [2,4,6]
✅ Solution — Two Pointers
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 += a[i:] + b[j:]
print(res)
🔴 Problem 12 — Anomaly Detection
scores = [10,12,11,50,13]
Anomaly > 2× average
✅ Solution — Pure
total = 0
for s in scores:
total += s
avg = total / len(scores)
res = []
for s in scores:
if s > 2 * avg:
res.append(s)
print(res)
🔴 Problem 13 — LRU Cache Simulation
Capacity = 2
access = ["A","B","A","C","B"]
✅ Solution — List Only
cache = []
for x in access:
if x in cache:
cache.remove(x)
elif len(cache) == 2:
cache.pop(0)
cache.append(x)
print(cache)
🔴 Problem 14 — Top-K Frequency (NO Counter)
events = ["login","pay","login","logout","login","pay"]
k = 2
✅ Solution — Manual Count + Sort
freq = {}
for e in events:
freq[e] = freq.get(e, 0) + 1
items = list(freq.items())
items.sort(key=lambda x: x[1], reverse=True)
print([x[0] for x in items[:k]])
🔴 Problem 15 — Smallest Window with 2 ERR
logs = ["OK","ERR","OK","ERR","ERR","OK"]
✅ Solution — Sliding Window
left = err = 0
best = float("inf")
for right in range(len(logs)):
if logs[right] == "ERR":
err += 1
while err >= 2:
best = min(best, right - left + 1)
if logs[left] == "ERR":
err -= 1
left += 1
print(best)
🔴 Problem 16 — Greedy Scheduling
tasks = [(3,10),(1,5),(2,7)]
✅ Solution
tasks.sort(key=lambda x: x[1])
time = 0
for d, dead in tasks:
time += d
if time > dead:
print(False)
break
else:
print(True)
🔴 Problem 17 — Cycle Detection (Graph)
deps = {"A":["B"],"B":["C"],"C":[]}
✅ Solution — DFS
vis, path = set(), set()
def dfs(n):
if n in path: return True
if n in vis: return False
path.add(n)
for nei in deps[n]:
if dfs(nei): return True
path.remove(n)
vis.add(n)
return False
print(any(dfs(x) for x in deps))
🔴 Problem 18 — Burst Detection (NO Counter)
t = [1,2,3,3,3,3,4]
✅ Solution — Manual Count
count = {}
for x in t:
count[x] = count.get(x, 0) + 1
if count[x] >= 4:
print(True)
break
else:
print(False)
🔴 Problem 19 — Streaming Median (Concept)
(Heap version optional — logic focus)
🔴 Problem 20 — Log Consistency
replicas = [["A","B","C"],["A","C"],["A","B","C","D"]]
✅ Solution — Intersection
common = set(replicas[0])
for r in replicas[1:]:
common &= set(r)
print(list(common))
🎯 Final Advice
If you can explain these out loud:
- You pass FAANG Python interviews
- You think like a real engineer
- You don’t memorize — you reason
🔥 This is interview maturity.