Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6212279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:29:16+00:00 2026-05-24T06:29:16+00:00

I saw this in an interview question , Given a sorting order string, you

  • 0

I saw this in an interview question ,
Given a sorting order string, you are asked to sort the input string based on the given sorting order string.
for example if the sorting order string is dfbcae
and the Input string is abcdeeabc
the output should be dbbccaaee.

any ideas on how to do this , in an efficient way ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:29:16+00:00Added an answer on May 24, 2026 at 6:29 am

    Here’s a nice easy to understand algorithm that has decent algorithmic complexity.

    For each character in the sort order string

    • scan string to be sorted, starting at first non-ordered character (you can keep track of this character with an index or pointer)
      • when you find an occurrence of the specified character, swap it with the first non-ordered character
      • increment the index for the first non-ordered character

    This is O(n*m), where n is the length of the string to be sorted and m is the length of the sort order string. We’re able to beat the lower bound on comparison based sorting because this algorithm doesn’t really use comparisons. Like Counting Sort it relies on the fact that you have a predefined finite external ordering set.

    Here’s some psuedocode:

    int head = 0;
    foreach(char c in sortOrder)
    {
        for(int i = head; i < sortTarget.length; i++)
        {
            if(sortTarget[i] == c)
            {
                 // swap i with head
                 char temp = sortTarget[head];
                 sortTarget[head] = sortTarget[i];
                 sortTarget[i] = temp;
    
                 head++;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I saw this question that asks given a string smith;rodgers;McCalne how can you produce
I saw this question asked about C# I would like an answer for PHP.
I recently saw an interview question asking the following: Given a 32 bit number,
Saw this question recently: Given 2 arrays, the 2nd array containing some of the
Saw this in a book: Given: class SortOf { String name; int bal; String
This is an interview question : How to serialize a graph ? I saw
This is an interview question I saw from here: http://www.careercup.com/question?id=1707701 Want to know more
This is an interview question I saw on some site. It was mentioned that
Hi All I saw this question posted on a site asked in a technical
I saw this in an answer to another question , in reference to shortcomings

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.