Array And String Algorithms
Asked of: Software Engineer
Last updated

What's being tested
These problems test linear array/string scanning, pointer discipline, and choosing the right optimization pattern instead of brute force. Expect to recognize monotonic stacks, sliding windows, interval sorting, bounded-range extrema, and in-place partitioning, then explain correctness and O(n) or O(n log n) complexity clearly.
Patterns & templates
-
Monotonic stack for next-smaller-or-equal discounts —
O(n)time, stack of candidate indices; watch equality semantics and rightward-only constraints. -
Sliding window for shortest constrained substring — expand right, update counts, contract left while valid;
O(n)with a frequency map. -
Bounded max/min window using deque — maintain candidates in decreasing/increasing order; expire indices outside window before computing profit.
-
Interval merge template — sort by start, track current
[start,end], merge whennext.start <= current.end; costsO(n log n). -
Dutch National Flag partitioning — three pointers
low,mid,high; sort three categories in-place inO(n)time andO(1)space. -
Invariant-first coding — state what each pointer/stack/deque represents before writing loops; this prevents off-by-one and stale-index bugs.
-
Complexity tradeoff — hash maps and stacks usually give
O(n)extra space; interval sorting dominates unless input is already sorted.
Common pitfalls
Pitfall: Treating “smaller” as strictly smaller when the discount rule requires smaller-or-equal changes answers on duplicate prices.
Pitfall: Shrinking a sliding window only once per right pointer often misses the shortest valid substring; contract in a
whileloop.
Pitfall: In three-way partitioning, incrementing
midafter swapping withhighskips an unclassified element.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Featured in interview prep guides
Practice questions
- Sort Three Categories In PlaceMicrosoft · Software Engineer · Technical Screen · medium
- Compute discounted prices and full-price indicesMicrosoft · Software Engineer · Technical Screen · hard
- Find all balanced k in a permutationMicrosoft · Software Engineer · Technical Screen · hard
- Find longest substring without repeating charactersMicrosoft · Software Engineer · Technical Screen · medium
- Merge overlapping intervalsMicrosoft · Software Engineer · Technical Screen · medium
- Find max consecutive elements with sum below targetMicrosoft · Software Engineer · Take-home Project · medium
- Find shortest substring with n unique lettersMicrosoft · Software Engineer · Onsite · medium
- Solve three scheduling and array problemsMicrosoft · Software Engineer · Onsite · medium
- Maximize profit with bounded sell windowMicrosoft · Software Engineer · Onsite · Medium
- Compute sum of longest positive subarrayMicrosoft · Software Engineer · Technical Screen · Medium
Related concepts
- String And Sliding Window AlgorithmsCoding & Algorithms
- Arrays, Strings, Hash Maps, And Sliding WindowsCoding & Algorithms
- Arrays, Sliding Windows, DP And Stack PatternsCoding & Algorithms
- Arrays, Intervals, Sliding Windows, And Prefix SumsCoding & Algorithms
- Arrays, Strings, And Matrix FundamentalsCoding & Algorithms
- Arrays, Strings, Hash Maps, And Frequency CountingCoding & Algorithms