Custom Sort a String
Updated
Problem statement
Given a string order whose distinct characters define a custom order, rearrange the characters of s to follow that order. Characters listed in order must appear in their custom order, with all copies of one listed character together.
For this exercise, put characters absent from order after all listed characters and keep those remaining characters in their original relative order from s. Return the resulting string.
Function
customSortString(order: String, s: String) → StringExamples
Example 1
order = "egbca"s = "abbceeeg"return = "eeegbbca"Custom order ranks e, then g, b, c, and a. Grouping the source characters in that order gives eeegbbca.
Example 2
order = "cba"s = "abcd"return = "cbad"The listed characters become cba. The unlisted d follows them.
Example 3
order = "bca"s = "xxacbxy"return = "bcaxxxy"Listed characters become bca. The unlisted characters retain their original sequence xxxy.
Constraints
1 <= order.length <= 26.1 <= s.length <= 200.- Both strings contain lowercase English letters.
- Every character in
orderis distinct.
More Meta Phone Screen questions
- Convert a Sorted Linked List to a Balanced BSTPHONE SCREEN · Seen Nov 2026
- Fuzzy Word Lookup With One Misspelled LetterPHONE SCREEN · Seen Nov 2026
- Multi-Head Attention Forward PassPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Run-Length Encoded Vector Dot ProductPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Mutual-Friend RecommendationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Time to Buy and Sell StockPHONE SCREEN · Seen Nov 2026
- First Element Greater Than KPHONE SCREEN · Seen Nov 2026
- Count Events in Rolling Minute WindowsPHONE SCREEN · Seen Oct 2026
- Valid Word AbbreviationPHONE SCREEN · Seen Oct 2026
- Shortest Path in a Binary MatrixPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Permutations IILinkedIn · PHONE SCREENLinkedIn interview questions
- Unique String PermutationsAmazon · PHONE SCREEN
- Rank Word-Translation Hints from Sentence TranslationsDuolingo · PHONE SCREEN · ONSITE INTERVIEWDuolingo interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Merge Weekday and Time IntervalsNextdoor · PHONE SCREENNextdoor interview questions
- Top K Movies by Play Count from CSV DataTubi · PHONE SCREEN
- Group AnagramsWorkday · PHONE SCREEN
- Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
- Apply Operations to Make a String EmptyFlexport · PHONE SCREENFlexport interview questions
- Bank Account Balances (Part 2): Monthly ReconciliationStripe · PHONE SCREENStripe interview questions
Meta interview experiences
- Meta PhD SWE Intern AI Coding Phone Screen: 1 Experience
- Meta Business Support Engineer Phone Screen: 1 Experience
- Meta Software Engineer Onsite Questions: 3 Experiences
- Meta AI Native Engineer OA and Onsite: 1 Experience
- Meta AI-Native Coding Interview: 3 Experiences
- Meta Production Engineer Intern Questions: 6 Experiences