Category
java
31 articles across 10 sub-topics
Big O Notation Explained: Time Complexity, Space Complexity, and Why They Matter in Interviews
TLDR: Big O notation describes how an algorithm's resource usage grows as input size grows — not how fast it runs on your laptop. Learn to identify the 7 complexity classes (O(1) through O(n!)), derive time and space complexity by counting loops and ...

Two Pointer Technique: Solving Pair and Partition Problems in O(n)
TLDR: Place one pointer at the start and one at the end of a sorted array. Move them toward each other based on a comparison condition. Every classic pair/partition problem that naively runs in O(n²)

Tries (Prefix Trees): The Data Structure Behind Autocomplete
TLDR: A Trie stores strings character by character in a tree, so every string sharing a common prefix shares those nodes. Insert and search are O(L) where L is the word length. Tries beat HashMaps on

Sliding Window Technique: From O(n·k) Scans to O(n) in One Pass
TLDR: Instead of recomputing a subarray aggregate from scratch on every shift, maintain it incrementally — add the incoming element, remove the outgoing element. For a fixed window this costs O(1) per

Merge Intervals Pattern: Solve Scheduling Problems with Sort and Sweep
TLDR: Sort intervals by start time, then sweep left-to-right and merge any interval whose start ≤ the current running end. O(n log n) time, O(n) space. One pattern — three interview problems solved.
In-Place Reversal of a Linked List: The 3-Pointer Dance Every Interviewer Expects
TLDR: Reversing a linked list in O(1) space requires three pointers — prev, curr, and next. Each step: save next, flip curr.next to point backward, advance both prev and curr. Learn this once and you unlock four reversal variants that appear constant...

Fast and Slow Pointer: Floyd's Cycle Detection Algorithm Explained
TLDR: Move a slow pointer one step and a fast pointer two steps through a linked structure. If they ever meet, a cycle exists. Then reset one pointer to the head and advance both one step at a time —
DFS — Depth-First Search: Go Deep Before Going Wide
TLDR: DFS explores a graph by diving as deep as possible along each path before backtracking, using a call stack (recursion) or an explicit stack. It is the go-to algorithm for cycle detection, path finding, topological sort, and connected components...
Cyclic Sort: Find Missing and Duplicate Numbers in O(n) Time, O(1) Space
TLDR: If an array holds n numbers in range [1, n], each number belongs at index num - 1. Cyclic sort places every element at its correct index in O(n) time using O(1) space — then a single scan reveals every missing and duplicate number. Five intervi...
Binary Search Patterns: Five Variants Every Senior Engineer Knows
TLDR: Binary search has five patterns beyond the classic "find the target": leftmost position, rightmost position, rotated array search, minimum in rotated array, and 2D matrix search. The root of every off-by-one bug is a mismatched loop condition a...
BFS — Breadth-First Search: Level-by-Level Graph Exploration
TLDR: BFS explores a graph level by level using a FIFO queue, guaranteeing the shortest path in unweighted graphs. Recognize BFS problems by keywords: "shortest path," "minimum steps," or "level order." Time: O(V + E). Space: O(V). Mark nodes visited...
Two Heaps Pattern: Find the Median of a Data Stream Without Sorting
TLDR: Two Heaps partitions a stream into two sorted halves. A max-heap holds everything below the median; a min-heap holds everything above it. Keep the heaps size-balanced and you can read the median from either top in O(1) — no sorting needed, ever...
Top K Elements Pattern: Find the Best K Without Sorting Everything
TLDR: To find the top K largest elements, maintain a min-heap of size K. For every new element, push it onto the heap. If the heap exceeds K, evict the minimum. After processing all N elements, the heap holds exactly the K largest. O(N log K) time — ...
K-Way Merge Pattern: Merge K Sorted Sequences with a Min-Heap
TLDR: K-Way Merge uses a min-heap with exactly one entry per sorted input list. Each entry stores the current element's value plus the coordinates to find the next element in that list. Pop the minimum (global smallest), append it to output, push the...
Simplifying Code with the Single Responsibility Principle
TLDR TLDR: The Single Responsibility Principle says a class should have only one reason to change. If a change in DB schema AND a change in email format both require you to edit the same class, that class has two responsibilities — and needs to be s...
Interface Segregation Principle: No Fat Interfaces
TLDR TLDR: The Interface Segregation Principle (ISP) states that clients should not be forced to depend on methods they don't use. Split large "fat" interfaces into smaller, role-specific ones. A RoboticDuck should not be forced to implement fly() j...
How the Open/Closed Principle Enhances Software Development
TLDR TLDR: The Open/Closed Principle (OCP) states software entities should be open for extension (add new behavior) but closed for modification (don't touch existing, tested code). This prevents new features from introducing bugs in old features. ...
Dependency Inversion Principle: Decoupling Your Code
TLDR TLDR: The Dependency Inversion Principle (DIP) states that high-level business logic should depend on abstractions (interfaces), not on concrete implementations (MySQL, SendGrid, etc.). This lets you swap a database or email provider without to...

Strategy Design Pattern: Simplifying Software Design
TLDR: The Strategy Pattern replaces giant if-else or switch blocks with a family of interchangeable algorithm classes. Each strategy is a self-contained unit that can be swapped at runtime without touching the client code. The result: Open/Closed Pri...
LLD for Parking Lot System: Designing a Smart Garage
TLDR TLDR: A Parking Lot is the "Hello World" of Low-Level Design. It teaches Encapsulation (ParkingFloor hides its Min-Heap), Abstraction (PricingStrategy interface), Inheritance (BikeSpot/CompactSpot/LargeSpot extend ParkingSpot), and Polymorphism...

LLD for Elevator System: Designing a Smart Lift
TLDR TLDR: An elevator system is a textbook OOP design exercise: ElevatorCar encapsulates its stop queue, ElevatorState polymorphically handles direction changes (State Pattern), and DispatchStrategy keeps assignment algorithms swappable (Strategy P...

LLD for Tic-Tac-Toe: Designing an Extensible OOP Game
TLDR: Tic-Tac-Toe looks trivial — until the interviewer says "make it N×N with P players and pluggable winning rules." The key design decisions: a Board abstracted from piece identity, a Strategy Pattern for win conditions, and a Factory for player c...

LLD for Ride Booking App: Designing Uber/Lyft
TLDR: A ride-booking system (Uber/Lyft-style) needs three interleaved sub-systems: real-time driver location tracking (Observer Pattern), nearest-driver matching (geospatial query), and dynamic pricing (Strategy Pattern). Getting state transitions ri...

Adapting to Virtual Threads for Spring Developers
TLDR: Platform threads (one OS thread per request) max out at a few hundred concurrent I/O-bound requests. Virtual threads (JDK 21+) allow millions — with zero I/O-blocking cost. Spring Boot 3.2 enables them with a single property. Avoid synchronized...
LLD for Movie Booking System: Designing BookMyShow
TLDR TLDR: A Movie Booking System (like BookMyShow) is an inventory management problem with an expiry: seats expire when the show starts. The core engineering challenge is preventing double-booking under concurrent user load with a 3-state seat mode...

Java 8 to Java 25: How Java Evolved from Boilerplate to a Modern Language
TLDR: Java went from the most verbose mainstream language to one of the most expressive. Lambdas killed anonymous inner classes. Records killed POJOs. Virtual threads killed thread pools for I/O work.
LLD for URL Shortener: Designing TinyURL
TLDR TLDR: A URL Shortener maps long URLs to short IDs. The core challenge is generating a globally unique, short, collision-free ID at scale. We use Base62 encoding on auto-incrementing database IDs for deterministic, collision-free short codes. ...
LLD for LRU Cache: Designing a High-Performance Cache
TLDR TLDR: An LRU (Least Recently Used) Cache evicts the item that hasn't been accessed the longest when it's full. The classic implementation combines a HashMap (O(1) lookup) with a Doubly Linked List (O(1) move-to-front) for overall O(1) get and p...

Java Memory Model Demystified: Stack vs. Heap
TLDR: Java memory is split into two main areas: the Stack for method execution frames and primitives, and the Heap for all objects. Understanding their differences is essential for avoiding stack overflow errors, memory leaks, and garbage collection ...
