Consolidated Logical & Technical Interview Questions for Backend Developers
Table of Contents
- Section 1: Logical & Coding Interview Questions
- Section 2: Python Backend Logical Q&A
- Section 3: Technical Interview Questions (100+ Expanded)
This document combines Logical & Coding Interview Questions, Python Backend Logical Q&A, and Technical Interview Questions into a single resource. It provides a comprehensive guide to prepare for backend interviews from basic to advanced levels.
Advanced Content Additions
Microservices Approach
Service Independent Deployability
Explain:
1. Designing a Scalable URL Shortener
Scenario
You are tasked with designing a backend for a URL shortener service (e.g., bit.ly). Provide a scalable architecture.
Key Considerations:
- URL encoding algorithms (Base62/Hashing).
- Database schema for short and long URLs.
- Optimizing traffic handling.
- Integrating persistent caching.
System Design:
- Frontend Load Balancer: Load distributes API requests.
- Service Tier: Scales horizontally during peak traffic.
- Database choices, sharding.
Concurrency Challenges
1. The "Flash Sale" Overselling Problem
Question:
You are building an e-commerce backend. You have a flash sale for a limited-edition sneaker with only 10 items in stock. At exactly 12:00 PM, 1,000 users click "Buy" simultaneously.
Logical Solution & Implementation
If you just read the stock and update, it causes Race Conditions. Use Pessimistic Locking (FOR UPDATE) to ensure consistent inventory updates.
from sqlalchemy.orm import Session
from sqlalchemy import text
def purchase_item(db: Session, item_id: int, user_id: int):
try:
db.begin()
item = db.execute(text("SELECT stock FROM items WHERE id = :id FOR UPDATE"),
{"id": item_id}).fetchone()
if item.stock <= 0:
db.rollback()
return "Out of stock"
db.execute(text("UPDATE items SET stock = stock - 1 WHERE id = :id"),
{"id": item_id})
db.execute(text("INSERT INTO orders (user_id, item_id) VALUES (:uid, :iid)"),
{"uid": user_id, "iid": item_id})
db.commit()
return "Success"
except Exception as e:
db.rollback()
raise e
Section 2: Python Backend Logical Q&A
Python Core Logical Questions
2. Custom Context Manager for Database Connections
Question:
Write a custom Context Manager for managing database connections with __enter__, __exit__.
Logical Solution:
Use Python’s with statement to ensure resources are properly acquired and released.
Universal Example: Pseudocode Controller
// reusableControllerExample
def __init__(self, db_url):
self.db_url = db_url
def __enter__(self):
print(f"Connecting to {self.db_url}")
self.conn = "mock_connection_object"
return self.conn
def __exit__(self, exc_type, exc_value, traceback):
print("Closing connection")
if exc_type:
print(f"Error occurred: {exc_value}")
return False # Propagate exceptions further
Section 3: Technical Interview Questions (100+ Expanded)
Algorithms and Logic
3. Bubble Sort
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
return arr
print(bubble_sort([64, 34, 25, 12, 22, 11, 90]))
4. Binary Search
def binary_search(arr, x):
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == x:
return mid
elif arr[mid] < x:
low = mid + 1
else:
high = mid - 1
return -1
print(binary_search([1, 2, 3, 4, 5], 4))
Note: This file is a sample/overview document containing representative Q&A from three source categories:
- Logical & coding problems (concurrency, design scenarios)
- Python backend logical questions (context managers, async patterns)
- Algorithm fundamentals (sorting, searching)
For the full question sets, refer to the topic-specific files in the numbered directories. See the introduction for the complete coverage table.