Variable naming in algorithms is a silent skill in coding interviews. Instead of naming a variable "i", if you give it a meaningful name like "lastKnownDuplicate", it will help you keep track of the meaning of that variable, like labeling storage boxes.
Here are some charts and tips to help you with naming variables as you prepare (with some help from ai and Grokking the Coding Interview).
🗂 Grokking Patterns + Variable Names (Quick Glance)
Pattern | Go-To Variable Names |
---|---|
Sliding Window |
windowStart , windowEnd , windowSum , charFrequency
|
Two Pointers |
left , right , targetSum , lastNonDuplicate
|
Fast & Slow Pointers |
slowPointer , fastPointer , cycleStart
|
Merge Intervals |
currentInterval , mergedIntervals , intervalStart , intervalEnd
|
Cyclic Sort |
currentIndex , correctIndex
|
In-place Linked List Reversal |
currentNode , previousNode , nextNode
|
Tree BFS |
queue , nodeQueue , currentNode , levelSize , levels
|
Tree DFS |
currentPath , allPaths , remainingSum
|
Two Heaps |
maxHeap , minHeap , median
|
Subsets / Backtracking |
currentSubset , subsets , startIndex
|
Modified Binary Search |
start , end , mid , target
|
Top K Elements |
minHeap , maxHeap , topKElements , distance
|
🗂 Grokking Patterns + Variable Names (In-Depth)
Sliding Window
Role | Good Name | Notes |
---|---|---|
Start index of window | windowStart |
First element in current range |
End index of window | windowEnd |
Last element in current range |
Frequency counter |
charFrequency , numFrequency
|
Use type-specific name |
Current sum/length |
windowSum , windowLength
|
Describes what’s being aggregated |
Two Pointers
Role | Good Name | Notes |
---|---|---|
Left pointer |
left or start
|
Works for arrays and strings |
Right pointer |
right or end
|
|
Target sum | targetSum |
Use when looking for sum |
Last valid position |
lastNonDuplicate , lastSorted
|
Clarifies purpose |
Fast & Slow Pointers
Role | Good Name | Notes |
---|---|---|
Slow pointer | slowPointer |
Advances 1 step at a time |
Fast pointer | fastPointer |
Advances 2 steps at a time |
Cycle start | cycleStart |
For cycle detection problems |
Merge Intervals
Role | Good Name | Notes |
---|---|---|
Current interval | currentInterval |
Often a tuple/array [start, end]
|
Merged result list | mergedIntervals |
Plural for clarity |
Interval start/end |
intervalStart , intervalEnd
|
Explicit meaning |
Cyclic Sort
Role | Good Name | Notes |
---|---|---|
Current index | currentIndex |
Avoid i
|
Correct index for value | correctIndex |
Makes swap logic obvious |
In-place Linked List Reversal
Role | Good Name | Notes |
---|---|---|
Current node | currentNode |
Where we are now |
Previous node | previousNode |
Node before current |
Next node | nextNode |
Node after current |
Tree BFS
Role | Good Name | Notes |
---|---|---|
Processing queue |
queue or nodeQueue
|
BFS uses queues |
Current node | currentNode |
|
Current level size | levelSize |
Used to group levels |
Result list |
result , levels
|
Use plural for collection |
Tree DFS
Role | Good Name | Notes |
---|---|---|
Current path | currentPath |
For backtracking |
All paths | allPaths |
For collecting results |
Remaining sum | remainingSum |
For path sum problems |
Two Heaps
Role | Good Name | Notes |
---|---|---|
Small half | maxHeap |
Stores smaller numbers |
Large half | minHeap |
Stores larger numbers |
Median | median |
Optional if caching |
Subsets / Backtracking
Role | Good Name | Notes |
---|---|---|
Current subset | currentSubset |
Or subset if short-lived |
Result list |
subsets or result
|
Plural for clarity |
Start index | startIndex |
For recursive calls |
Modified Binary Search
Role | Good Name | Notes |
---|---|---|
Search start | start |
Lower bound index |
Search end | end |
Upper bound index |
Midpoint | mid |
Middle index |
Target value | target |
What we’re searching for |
Top K Elements
Role | Good Name | Notes |
---|---|---|
Heap |
minHeap or maxHeap
|
Match name to type used |
Result list | topKElements |
Describes final goal |
Distance function | distance |
For closest point problems |
🗂 Grokking Patterns + Variable Names (Quick Tips)
-
Role + Context
Instead of i, say what it is and why it’s there.❌ i → ✔ windowStart
❌ j → ✔ windowEnd
❌ count → ✔ charFrequency -
Relationship to Other Variables
If there’s a pair, name them so they “talk to each other.”fastPointer / slowPointer
minHeap / maxHeap
left / right -
Avoid Mental Gymnastics
If a name makes you stop and translate it in your head, it’s too short or too vague.❌ ptr → ✔ currentNode
❌ n1, n2 → ✔ intervalStart, intervalEnd
Top comments (0)