FastPrepCustom Sort a String
Problems/Custom Sort a String

Custom Sort a String

Meta logoMeta● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A Meta (Facebook) ML researcher reports a Custom Sort String task in a phone screen. The example was abbceeeg ordered by egbca, giving eeegbbca. The interviewer talked about an O(n log n) approach and a faster possibility. Our version uses the limits of the linked public task. It also sets where characters go if they are not in the custom order, which the report did not say. Our version is about 85% the same as the reported question.

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) → String

Examples

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 order is 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
All Meta interview questionsSee Meta hiring insights
Saved
class Solution {
    public String customSortString(String order, String s) {
        // Write your code here
    }
}
Checking account…