The Number of Adjacent Elements With the Same Color problem gives you an array (initially all uncolored), a number of colors, and a sequence of color assignments. After each assignment, report how many adjacent pairs have the same color. This coding problem maintains a running count efficiently rather than recomputing from scratch after each update.
Uber, Visa, Roblox, Amazon, TikTok, and Capital One ask this to test efficient count maintenance under array updates. The naive O(n) recount per query is too slow — the O(1) per update solution requires tracking which adjacent pairs are affected by each assignment. The array interview pattern is demonstrated with incremental update reasoning.
Incremental update with neighbor check. Maintain a running count of same-color adjacent pairs. For each assignment (index, color): before assigning, subtract the contribution of the current element's existing adjacent matches (check left neighbor and right neighbor). After assigning the new color, add the new adjacent matches. This updates count in O(1) per query.
Array (0-indexed): initially [0,0,0,0] (0=uncolored). count=0.
Incremental update problems require identifying what changes locally around each modification. For adjacency problems: only the left-pair and right-pair of the modified element can change. Calculate the "before" contribution, apply the update, calculate the "after" contribution, and adjust the running count accordingly. This O(1)-per-update pattern generalizes to any array query where modifications have bounded local effects — practice it with "count of equal pairs" and "count inversions under updates."
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Sum of Beauty in the Array | Medium | Solve | |
| Insert Interval | Medium | Solve | |
| Maximize Distance to Closest Person | Medium | Solve | |
| Maximum Value of an Ordered Triplet II | Medium | Solve | |
| All Divisions With the Highest Score of a Binary Array | Medium | Solve |