← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Meta mobile engineer interview with a coding round that included the Custom Sort String problem. Pretty standard algorithmic stuff, nothing too wild.

Questions Asked (1)

Q1

Given a custom ordering of characters and a string, sort the string so that characters appear in the order defined by the custom ordering. Characters not in the custom order can go anywhere at the end.

Algorithms & Data Structures
Author's notes

I went with a frequency map approach, built a count of each character in the string, then iterated through the order string to reconstruct.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem constraints (e.g., custom order length, string size, character set) and discuss trade-offs between comparison-based sorting and counting-based approaches. Propose an efficient solution using a hash map to store the custom order indices and a stable sort with a custom comparator, or counting sort if the character set is small. Walk through an example to demonstrate correctness and analyze time/space complexity.

Pro tip: Mention that characters not in the custom order can be placed at the end by assigning them a default index larger than any custom order index, and highlight that a counting sort approach can achieve O(n + k) time where k is the size of the custom alphabet, which is optimal for large strings.

1. Clarify requirements and constraints

Ask about the size of the custom order, the character set (ASCII, Unicode), and whether the string can contain characters not in the custom order. Confirm that characters not in the custom order should appear at the end, and discuss if their relative order matters.

2. Choose an approach

Decide between comparison-based sorting with a custom comparator (O(n log n)) and counting sort (O(n + k)). Consider the trade-offs: counting sort is faster but requires extra space proportional to the custom alphabet size.

3. Implement the solution

For counting sort: create a frequency map of characters in the string, then iterate through the custom order, appending each character repeated by its frequency. Finally, append any remaining characters not in the custom order. For comparator sort: map each character to its index in the custom order (or a large number if absent) and sort the string using that mapping.

4. Test with edge cases

Test with empty string, string with only characters not in custom order, custom order containing all characters, and duplicate characters. Verify that the output maintains the custom order and that extra characters are at the end.

5. Analyze complexity and optimize

State the time and space complexity of your solution. For counting sort, it's O(n + k) time and O(k) space, where n is string length and k is custom order length. Discuss potential optimizations, such as using an array instead of a hash map if the character set is small.

Key Points to Mention

  • Hash map to store the custom order indices for O(1) lookup.
  • Stable sorting to preserve the relative order of characters not in the custom order (if required).
  • Counting sort approach for O(n + k) time complexity, which is optimal for large strings.
  • Handling characters not in the custom order by assigning them a default index (e.g., infinity) or appending them after sorting.
  • Edge cases: empty string, all characters not in custom order, custom order with duplicates (should be handled by using first occurrence).
  • Space-time trade-off: counting sort uses extra space but is faster; comparison sort is in-place but slower.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.