Interview Question

Country: United States

Comment hidden because of low score. Click to expand.
of 0 vote

Initially I thought about this as simply removing non-alphabetic characters before normal sorting, but that doesn't account for the length of words. (In the provided example, all words are three characters so that would work). To account for length (in particular, when one word is a prefix of another - as in dictionary order, as opposed to shortlex order), words need to be compared one by one. Which means, the order is determined by:

1. The first alphabetic character that doesn't match.
2. If all "prefix" letters match, the longer word wins.
3. If words are tied, proceed to the next word.
4. If all else fails, the longest list wins.

Aside from just tracking indexes manually (to skip non-letter characters in a word, such as apostrophes), this comparison is straightforward (and runs in O(N) time on the length of the string).

After you've got a comparison function, sorting is straightforward. Here's my implementation in Javascript (using the built-in Array.sort() routine.) I've also included a few longer and shorter examples to show that this sort acts appropriately.

function lexCompare(str1,str2) {
    var words1 = str1.split(" ").filter(s=>s!='');
    var words2 = str2.split(" ").filter(s=>s!='');

    for (let i=0; i < Math.min(words1.length, words2.length); i++) {
        var p = 0, q = 0; // letter indices
        word1 = words1[i].toLowerCase();
        word2 = words2[i].toLowerCase();
        while (p < word1.length && q < word2.length) {
            var char1 = word1[p], char2 = word2[q];
            // Skip non-letter characters.
            if (!char1.match(/[a-z]/)) { p++; continue; }
            if (!char2.match(/[a-z]/)) { q++; continue; }
            // if non-matching letter, order has been determined.
            if (char1 < char2) { return -1; }
            if (char1 > char2) { return  1; }
            // Move on to next letter.
            p++; q++;
        // words match at least to subset, check if same length.
        if (word1.length < word2.length) { return -1; }
        if (word1.length > word2.length) { return  1; }
    // All words exactly matched at least to subset, longest list wins.
    if (words1.length > words2.length) { return -1; }
    if (words1.length < words2.length) { return  1; }
    // And if all else fails, tie.
    return 0;

elements = [ "act car", "air dog", "act zoo", "action zoo", "action dog", "act ca" ]
document.write(elements.join("<br />"));

- Jack November 30, 2019 | Flag Reply

Add a Comment

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.


is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More


CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More