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 6548777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:00:40+00:00 2026-05-25T12:00:40+00:00

I was recently asked the following interview question: You have a dictionary page written

  • 0

I was recently asked the following interview question:

You have a dictionary page written in an alien language. Assume that
the language is similar to English and is read/written from left to
right. Also, the words are arranged in lexicographic order. For
example the page could be: ADG, ADH, BCD, BCF, FM, FN
You have to give all lexicographic orderings possible of the character
set present in the page.

My approach is as follows:
A has higher precedence than B and G has higher precedence than H.
Therefore we have the information about ordering for some characters:

A->B, B->F, G->H, D->F, M->N

The possible orderings can be ABDFGNHMC, ACBDFGNHMC, …
My approach was to use an array as position holder and generate all permutations to identify all valid orderings. The worst case time complexity for this is N! where N is the size of character set.
Can we do better than the brute force approach.

Thanks in advance.

  • 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-25T12:00:41+00:00Added an answer on May 25, 2026 at 12:00 pm

    Donald Knuth has written the paper A Structured Program to Generate all Topological Sorting Arrangements. This paper was originally pupblished in 1974. The following quote from the paper brought me to a better understanding of the problem (in the text the relation i < j stands for “i precedes j”):

    A natural way to solve this problem is to let x1 be an
    element having no predecessors, then to erase all relations of the
    from x1 < j and to let x2 be an element ≠
    x1 with no predecessors in the system as it now exists,
    then to erase all relations of the from x2 < j , etc. It is
    not difficult to verify that this method will always succeed unless
    there is an oriented cycle in the input. Moreover, in a sense it is
    the only way to proceed, since x1 must be an element
    without predecessors, and x2 must be without predecessors
    when all relations x1 < j are deleted, etc. This
    observation leads naturally to an algorithm that finds all
    solutions to the topological sorting problem; it is a typical example
    of a “backtrack” procedure, where at every stage we consider a
    subproblem of the from “Find all ways to complete a given partial
    permutation x1x2…xk to a
    topological sort x1x2…xn .” The
    general method is to branch on all possible choices of
    xk+1.
    A central problem in backtrack applications is
    to find a suitable way to arrange the data so that it is easy to
    sequence through the possible choices of xk+1 ; in this
    case we need an efficient way to discover the set of all elements ≠
    {x1,…,xk} which have no predecessors other
    than x1,…,xk, and to maintain this knowledge
    efficiently as we move from one subproblem to another.

    The paper includes a pseudocode for a efficient algorithm. The time complexity for each output is O(m+n), where m ist the number of input relations and n is the number of letters. I have written a C++ program, that implements the algorithm described in the paper – maintaining variable and function names –, which takes the letters and relations from your question as input. I hope that nobody complains about giving the program to this answer – because of the language-agnostic tag.

    #include <iostream>
    #include <deque>
    #include <vector>
    #include <iterator>
    #include <map>
    
    // Define Input
    static const char input[] =
        { 'A', 'D', 'G', 'H', 'B', 'C', 'F', 'M', 'N' };
    static const char crel[][2] =
        {{'A', 'B'}, {'B', 'F'}, {'G', 'H'}, {'D', 'F'}, {'M', 'N'}};
    
    static const int n = sizeof(input) / sizeof(char);
    static const int m = sizeof(crel) / sizeof(*crel);
    
    std::map<char, int> count;
    std::map<char, int> top;
    std::map<int, char> suc;
    std::map<int, int> next;
    std::deque<char> D;
    std::vector<char> buffer;
    
    void alltopsorts(int k)
    {
        if (D.empty())
            return;
        char base = D.back();
    
        do
        {
            char q = D.back();
            D.pop_back();
    
            buffer[k] = q;
            if (k == (n - 1))
            {
                for (std::vector<char>::const_iterator cit = buffer.begin();
                     cit != buffer.end(); ++cit)
                     std::cout << (*cit);
                std::cout << std::endl;
            }
    
            // erase relations beginning with q:
            int p = top[q];
            while (p >= 0)
            {
                char j = suc[p];
                count[j]--;
                if (!count[j])
                    D.push_back(j);
                p = next[p];
            }
    
            alltopsorts(k + 1);
    
            // retrieve relations beginning with q:
            p = top[q];
            while (p >= 0)
            {
                char j = suc[p];
                if (!count[j])
                    D.pop_back();
                count[j]++;
                p = next[p];
            }
    
            D.push_front(q);
        }
        while (D.back() != base);
    }
    
    int main()
    {
        // Prepare
        std::fill_n(std::back_inserter(buffer), n, 0);
        for (int i = 0; i < n; i++) {
            count[input[i]] = 0;
            top[input[i]] = -1;
        }
    
        for (int i = 0; i < m; i++) {
            suc[i] = crel[i][1]; next[i] = top[crel[i][0]];
            top[crel[i][0]] = i; count[crel[i][1]]++;
        }
    
        for (std::map<char, int>::const_iterator cit = count.begin();
             cit != count.end(); ++cit)
            if (!(*cit).second)
                D.push_back((*cit).first);
    
        alltopsorts(0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was asked following question in interview recently: Let suppose you have, following grid
Recently I was asked the following question at interview. What are the different way
Recently, I have been asked a question in an interview what's the difference between
Recently in a job interview I was asked this following question (for Java): Given:
I recently attended an interview and was asked the following question. There are two
Friend of mine got asked the following question in an interview recently and im
Recently I was asked the following questions in an interview: How will you do
Please, help me clear my mind on the following question. Recently I asked a
I have recently been receiving the following error whenever I am asked to supply
I recently asked a question about functional programming, and received (good!) answers that prompted

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.