dsa2 min read
Merge Intervals — Sort + Linear Scan O(n log n) [Google, Meta, Amazon]
Merge all overlapping intervals. Sort by start, then merge greedily in one pass. O(n log n) time.
Read →
webcoderspeed.com
1276 articles
Merge all overlapping intervals. Sort by start, then merge greedily in one pass. O(n log n) time.
Return all elements of an m×n matrix in spiral order. Use boundary shrinking or direction array. O(m*n) time O(1) space.
Group strings that are anagrams of each other. Use sorted string as HashMap key. O(n*k*log k) time. Full solutions in 5 languages.
Find length of longest substring without repeating characters. Optimal O(n) sliding window with HashMap tracking last seen positions.
Count subarrays summing to k. Classic prefix sum + HashMap trick: if prefix[j]-prefix[i]=k then prefix[i]=prefix[j]-k. O(n) time.