Arrays, Strings, Hashing, And Two Pointers
Asked of: Software Engineer
Last updated
What's being tested
These problems test hashing, two pointers, sliding-window reasoning, and careful string parsing over arrays and text. Strong solutions identify whether order, contiguity, duplicates, circular indexing, or exact token boundaries determine the appropriate O(n) or O(n log n) strategy.
Patterns & templates
-
Two pointers on sorted arrays — move the left pointer upward or right pointer downward based on the sum; handle duplicates deliberately.
-
Hash maps and sets — track frequencies, last-seen positions, or distinct characters for linear-time substring and pair-counting solutions.
-
Contribution counting — count how many substrings include each character’s unique contribution instead of enumerating all
O(n²)substrings. -
Prefix sums for circular distances — duplicate or modularize indices, then compute interval distance as
prefix[r] - prefix[l]. -
Monotonic boundary scans — find the first inversion from the left and right, expand around the unsorted region, then validate the repaired range.
-
Finite-state parsing — recognize complete price tokens using character boundaries; avoid matching
$12inside$123or unrelated text. -
Complexity discipline — prefer
O(n)time andO(n)space; use sorting only when itsO(n log n)cost simplifies correctness and duplicate handling.
Common pitfalls
Pitfall: Treating circular routes like linear arrays; compare clockwise and counterclockwise distances and normalize with modulo.
Pitfall: Counting distinct substring spellings rather than occurrences by position, or accidentally using 32-bit arithmetic for large totals.
Pitfall: Moving both two-pointer indices after every match without grouping duplicates, causing missed pairs or repeated output.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Practice questions
- Minimum Adjacent Swaps to Group a Binary ArrayAmazon · Software Engineer · Online Assessment · medium
- Discount Only Complete Dollar-Price Words in a SentenceAmazon · Software Engineer · Other · medium
- Add Distinct-Letter Counts across Every SubstringAmazon · Software Engineer · Other · hard
- Partition a String into the Fewest Duplicate-Free PiecesAmazon · Software Engineer · Other · medium
- Calculate Circular Route Query DistanceAmazon · Software Engineer · Technical Screen · medium
- Group Anagrams Without Sorted Character SignaturesAmazon · Software Engineer · Online Assessment · medium
- Find the K Most Frequent ValuesAmazon · Software Engineer · Online Assessment · medium
- Find all pairs summing to target in sorted arrayAmazon · Software Engineer · Technical Screen · medium
- Find shortest subarray to restore orderAmazon · Software Engineer · Technical Screen · medium
- Find substring from dictionary concatenationAmazon · Software Engineer · Technical Screen · medium
- Implement basic calculatorAmazon · Software Engineer · Online Assessment · medium
Related concepts
- Arrays, Strings, Hash Maps, And Frequency CountingCoding & Algorithms
- Arrays, Strings And Hash CountingCoding & Algorithms
- Arrays, Strings, Hash Maps, And Sliding WindowsCoding & Algorithms
- Arrays, Strings, and HashingCoding & Algorithms
- Core Array, String, Hash Map, Sliding Window, and Binary Search PatternsCoding & Algorithms
- Arrays, Sliding Windows, DP And Stack PatternsCoding & Algorithms