Arrays, Strings And Hash Counting
Asked of: Software Engineer
Last updated
What's being tested
These problems test frequency counting, hash-based lookup, prefix invariants, and careful distinctions between subsequences, substrings, partitions, and anagrams. You should demonstrate how to reduce brute force with counters, sets, or structured scans while stating time and space complexity and handling empty inputs, repeated characters, and boundary positions.
Patterns & templates
-
Frequency signature for anagrams — count characters with a fixed array or map; grouping is
O(nm)fornstrings of lengthm, plus output storage. -
Hash map lookup — store counts, last positions, or canonical keys for expected
O(1)average access; distinguish worst-case guarantees from practical performance. -
Prefix invariant — after each update, verify whether every position in a required prefix is active; track the current prefix boundary instead of rescanning all positions.
-
Greedy partitioning — maintain a character set for the current repeat-free piece; when a duplicate appears, close the piece and reset in
O(n)time. -
Two-pointer subsequence scan — advance through the candidate substring while matching characters from the source; subsequence matching is non-contiguous, substring matching is contiguous.
-
Deletion and substitution evaluation — enumerate each candidate removed character or local replacement, then scan efficiently; avoid mutating the original string repeatedly.
-
Complexity discipline — state whether
O(nm),O(n + alphabet), orO(n²)is acceptable, and include hash-map space plus result/output space.
Common pitfalls
Pitfall: Treating anagrams as sorted strings everywhere can add
O(m log m)per string; frequency vectors are typically faster and deterministic for bounded alphabets.
Pitfall: Confusing a subsequence with a substring leads to invalid matches; explicitly track whether selected characters must remain contiguous.
Pitfall: Rechecking every prefix or partition from scratch hides an avoidable quadratic loop; maintain an invariant and update only the affected state.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Practice questions
- Fewest Repeat-Free Pieces After Deleting Every Copy of One LetterSalesforce · Software Engineer · Online Assessment · medium
- Sum a Matrix After Row and Column AssignmentsSalesforce · Software Engineer · Other · medium
- Generate All Balanced Parenthesis Strings with n Pairs in Sorted OrderSalesforce · Software Engineer · Technical Screen · medium
- Validate a Generalized Word AbbreviationSalesforce · Software Engineer · Onsite · medium
- Generate Every Generalized Abbreviation of a WordSalesforce · Software Engineer · Onsite · medium
- Solve Two OA Coding ProblemsSalesforce · Software Engineer · Online Assessment · medium
- Count the Steps at Which Set Bits Form an Exact Prefix of OnesSalesforce · Software Engineer · Other · medium
- Solve array duplicate flags and binary swapsSalesforce · Software Engineer · Online Assessment · medium
- Find longest subsequence of x that is a substring of ySalesforce · Software Engineer · HR Screen · medium
- Flatten nested JSON into a string mapSalesforce · Software Engineer · Onsite · medium
- Design O(nm) grouping of rearranged stringsSalesforce · Software Engineer · Technical Screen · medium
- Determine minimal substitutions and removalsSalesforce · Software Engineer · Online Assessment · medium
Related concepts
- Arrays, Strings, Hash Maps, And Frequency 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
- Core Data Structures, Sorting, And ComplexityCoding & Algorithms
- Arrays, Sliding Windows, DP And Stack PatternsCoding & Algorithms