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

  • Home
  • SEARCH
  • 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 7990101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:54:42+00:00 2026-06-04T12:54:42+00:00

This is an interview question (phone screen): write a function (in Java) to find

  • 0

This is an interview question (phone screen): write a function (in Java) to find all permutations of a given word that appear in a given text. For example, for word abc and text abcxyaxbcayxycab the function should return abc, bca, cab.

I would answer this question as follows:

  • Obviously I can loop over all permutations of the given word and use a standard substring function. However it might be difficult (for me right now) to write code to generate all word permutations.

  • It is easier to loop over all text substrings of the word size, sort each substring and compare it with the “sorted” given word. I can code such a function immediately.

  • I can probably modify some substring search algorithm but I do not remember these algorithms now.

How would you answer this question?

  • 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-04T12:54:43+00:00Added an answer on June 4, 2026 at 12:54 pm

    This is probably not the most efficient solution algorithmically, but it is clean from a class design point of view. This solution takes the approach of comparing “sorted” given words.

    We can say that a word is a permutation of another if it contains the same letters in the same number. This means that you can convert the word from a String to a Map<Character,Integer>. Such conversion will have complexity O(n) where n is the length of the String, assuming that insertions in your Map implementation cost O(1).

    The Map will contain as keys all the characters found in the word and as values the frequencies of the characters.

    Example. abbc is converted to [a->1, b->2, c->1]

    bacb is converted to [a->1, b->2, c->1]

    So if you have to know if two words are one the permutation of the other, you can convert them both into maps and then invoke Map.equals.

    Then you have to iterate over the text string and apply the transformation to all the substrings of the same length of the words that you are looking for.

    Improvement proposed by Inerdial

    This approach can be improved by updating the Map in a “rolling” fashion.

    I.e. if you’re matching at index i=3 in the example haystack in the OP (the substring xya), the map will be [a->1, x->1, y->1]. When advancing in the haystack, decrement the character count for haystack[i], and increment the count for haystack[i+needle.length()].

    (Dropping zeroes to make sure Map.equals() works, or just implementing a custom comparison.)

    Improvement proposed by Max

    What if we also introduce matchedCharactersCnt variable? At the beginning of the haystack it will be 0. Every time you change your map towards the desired value – you increment the variable. Every time you change it away from the desired value – you decrement the variable. Each iteration you check if the variable is equal to the length of needle. If it is – you’ve found a match. It would be faster than comparing the full map every time.

    Pseudocode provided by Max:

    needle = "abbc"
    text = "abbcbbabbcaabbca"
    
    needleSize = needle.length()
    //Map of needle character counts
    targetMap = [a->1, b->2, c->1]
    
    matchedLength = 0
    curMap = [a->0, b->0, c->0]
    //Initial map initialization
    for (int i=0;i<needle.length();i++) {
        if (curMap.contains(haystack[i])) {
            matchedLength++
            curMap[haystack[i]]++
        }
    }
    
    if (matchedLength == needleSize) {
        System.out.println("Match found at: 0");
    }
    
    //Search itself
    for (int i=0;i<haystack.length()-needle.length();i++) {
        int targetValue1 = targetMap[haystack[i]]; //Reading from hashmap, O(1)
        int curValue1 = curMap[haystack[i]]; //Another read
        //If we are removing beneficial character
        if (targetValue1 > 0 && curValue1 > 0 && curValue1 <= targetValue1) {       
            matchedLength--;
        }
        curMap[haystack[i]] = curValue1 + 1; //Write to hashmap, O(1)
    
    
        int targetValue2 = targetMap[haystack[i+needle.length()]] //Read
        int curValue2 = curMap[haystack[i+needle.length()]] //Read
        //We are adding a beneficial character
        if (targetValue2 > 0 && curValue2 < targetValue2) { //If we don't need this letter at all, the amount of matched letters decreases
            matchedLength++;
        }
        curMap[haystack[i+needle.length()]] = curValue2 + 1; //Write
    
        if (matchedLength == needleSize) {
            System.out.println("Match found at: "+(i+1));
        }
    }
    
    //Basically with 4 reads and 2 writes which are 
    //independent of the size of the needle,
    //we get to the maximal possible performance: O(n)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is an interview question: Given a string, find all its permutations that are
I was asked this question during phone interview. Given two strings find the minimal
This was an interview question: Given an amount, say $167.37 find all the possible
This is an interview Question that i was asked recently: Write a C program
This is an interview question : Given a directory with lots of files, find
This is an interview question I faced recently. Given an array of 1 and
Just finished reading this blog post: http://www.skorks.com/2010/03/an-interview-question-that-prints-out-its-own-source-code-in-ruby/ In it, the author argues the case
I was given this interview question recently: Given a 12-hour analog clock, compute in
this was an interview question posed to me..I vaguely answered it uses Java reflections..but
The Title is self explanatory. This was an interview question. In java, List is

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.