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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:21:59+00:00 2026-06-07T10:21:59+00:00

This is an interview question. Given a number of strings find such strings, which

  • 0

This is an interview question. Given a number of strings find such strings, which are prefixes of others. For example, given strings = {"a", "aa", "ab", abb"} the result is {"a", "ab"}.

The simplest solution is just to sort the strings and check each pair of two subsequent strings if the 1st one is a prefix of the 2nd one. The running time of the algorithm is the running time of the sorting.

I guess there is another solution, which uses a trie, and has complexity O(N), where N is the number of strings. Could you suggest such an algorithm?

  • 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-06-07T10:22:01+00:00Added an answer on June 7, 2026 at 10:22 am

    I have a following idea regarding Trie, complexity O(N):
    You start with empty Trie.
    You take words one by one, and add word to Trie.
    After you add a word (let’s call it word Wi) to Trie, there are two cases to consider:

    1. Wi is prefix of some of the words you added before.
      That statement is true if you didn’t add any nodes to Trie while adding word Wi.
      In that case, Wi is prefix and part of our solution.
    2. Some of the words added before are prefix of Wi.
      That statement is true if you passed through node that represents end of some word added before (let’s cal that word Wj). In that case, Wj is prefix of Wi and part of our solution.

    In more details (pseudocode):

    for word in words
        add word to trie
        if size of trie did not change then   // first case
            add word to result
        if ending nodes found while adding word   // second case
            add words defined by those nodes to result
    return result
    

    Adding new word to Trie:

    node = trie.root();
    for letter in word
        if node.hasChild(letter) == false then   // if letter doesnt exist, add it
            node.addChild(letter)
        if letter is last_letter_of_word then   // if last letter of word, store that info
            node.setIsLastLetterOf(word)
        node = node.getChild(letter)    // move
    

    While you are adding new word, you can also check if you passed through any nodes that represent last letters of other words.
    Complexity of algorithm that I described is O(N).
    Another important thing is that this way you can know how many times word Wi prefixes other words, which may be useful.

    Example for {aab, aaba, aa}:
    Green nodes are nodes detected as case 1.
    Red nodes are nodes detected as case 2.
    Each column(trie) is one step. At the beginning trie is empty.
    Black arrows show which nodes we visited(added) in that step.
    Nodes that represent last letter of some word have that word written in parenthesess.

    enter image description here

    1. In step 1 we add word aab.
    2. In step 2 we add word aaba, recognize one case 2 (word aab) and add word aab to result.
    3. In step 3 we add word aa, recognize case 1 and add word aa to result.

    At the end we have result = {aab, aa} which is correct.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was asked this question during phone interview. Given two strings find the minimal
I was given this coding question in interview: given a very very large number
I came across this post , which reports the following interview question: Given two
This is an interview question . Given an array of numbers and another number,
This is an interview question: Given a string, find all its permutations that are
This is an interview question. Given a string such as: 123456abcdef consisting of n/2
This was an interview question: Given an amount, say $167.37 find all the possible
Possible Duplicate: Easy interview question got harder: given numbers 1..100, find the missing number(s)
This is an interview question I faced recently. Given an array of 1 and
This is an interview question. I have K machines each of which is connected

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.