PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This interview question evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer for Sort a string by custom order states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

  • Medium
  • Meta
  • Coding & Algorithms
  • Software Engineer

Sort a string by custom order

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

Given a priority string P of distinct characters and a text string S, reorder S so that all characters appearing in P come first and are grouped in the exact order specified by P, followed by all characters not in P while preserving their original relative order. Return the reordered string. Design an O(|P| + |S|) algorithm and discuss space usage. How would you adapt the solution for Unicode and for case-insensitive ordering?

Quick Answer: This interview question evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer for Sort a string by custom order states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

Given a priority string `P` of distinct characters and a text string `S`, reorder `S` so that: 1. All characters of `S` that appear in `P` come first, grouped together in the exact order specified by `P` (all occurrences of `P[0]`, then all occurrences of `P[1]`, and so on). 2. They are followed by all characters of `S` that do NOT appear in `P`, preserving their original relative order from `S`. Return the reordered string. Characters in `P` are guaranteed distinct. Characters may repeat in `S`. Aim for an `O(|P| + |S|)` time algorithm. **Example 1** ``` P = "cba", S = "abcd" Output: "cbad" ``` Characters 'a','b','c' all appear in P, so they are ordered as c, b, a. 'd' is not in P, so it comes last. **Example 2** ``` P = "kqep", S = "pekeq" Output: "kqeep" ``` Counts: k=1, q=1, e=2, p=1. Emit in P order: k, q, e, e, p. **Example 3** ``` P = "ba", S = "aabbcc" Output: "bbaacc" ``` 'b' before 'a' per P; 'c' not in P keeps its trailing order.

Constraints

  • Characters in P are distinct.
  • S may contain repeated characters.
  • Either P or S may be empty.
  • If P is empty, return S unchanged.
  • Characters of S not in P must preserve their original relative order.

Examples

Input: ("cba", "abcd")

Expected Output: "cbad"

Explanation: a,b,c are in P -> ordered c,b,a; d not in P -> last.

Input: ("bcafg", "abcd")

Expected Output: "bcad"

Explanation: P order b,c,a; d is not in P so it trails. f,g in P are absent from S and ignored.

Input: ("", "hello")

Expected Output: "hello"

Explanation: Empty priority string: no character is in P, so S is returned unchanged in original order.

Input: ("xyz", "")

Expected Output: ""

Explanation: Empty text string returns empty.

Input: ("ba", "aabbcc")

Expected Output: "bbaacc"

Explanation: Duplicates: b (x2) before a (x2) per P; c (x2) not in P keeps trailing order.

Input: ("kqep", "pekeq")

Expected Output: "kqeep"

Explanation: Counts k=1,q=1,e=2,p=1 emitted in P order -> k,q,e,e,p.

Hints

  1. Build a hash set (or map) of the characters in P so you can test membership in O(1).
  2. Single pass over S: count occurrences of each character that is in P, and append (in order) characters that are not in P to a separate buffer.
  3. Emit the result by walking P once and writing each character as many times as it was counted, then append the not-in-P buffer.
  4. For Unicode: iterate over code points (Python str already does; in Java/C++ decode UTF-8/UTF-16 to code points) rather than bytes, and key the count map by code point. For case-insensitive ordering, normalize both P and the matched characters with a consistent case fold before comparing, but emit the original characters.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Find Shortest Unique Prefixes - Meta (medium)
  • Compute Exclusive Execution Times - Meta (medium)
  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve Tree Diameter and Palindromic Counts - Meta (medium)
  • Simulate Monster Team Battles - Meta (hard)