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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:22:47+00:00 2026-05-21T23:22:47+00:00

I was asked this question in a phone interview for summer internship, and tried

  • 0

I was asked this question in a phone interview for summer internship, and tried to come up with a n*m complexity solution (although it wasn’t accurate too) in Java.

I have a function that takes 2 strings, suppose “common” and “cmn”. It should return True based on the fact that ‘c’, ‘m’, ‘n’ are occurring in the same order in “common”. But if the arguments were “common” and “omn”, it would return False because even though they are occurring in the same order, but ‘m’ is also appearing after ‘o’ (which fails the pattern match condition)

I have worked over it using Hashmaps, and Ascii arrays, but didn’t get a convincing solution yet! From what I have read till now, can it be related to Boyer-Moore, or Levenshtein Distance algorithms?

Hoping for respite at stackoverflow! 🙂

Edit: Some of the answers talk about reducing the word length, or creating a hashset. But per my understanding, this question cannot be done with hashsets because occurrence/repetition of each character in first string has its own significance. PASS conditions- “con”, “cmn”, “cm”, “cn”, “mn”, “on”, “co”. FAIL conditions that may seem otherwise– “com”, “omn”, “mon”, “om”. These are FALSE/FAIL because “o” is occurring before as well as after “m”. Another example- “google”, “ole” would PASS, but “google”, “gol” would fail because “o” is also appearing before “g”!

  • 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-21T23:22:48+00:00Added an answer on May 21, 2026 at 11:22 pm

    I think it’s quite simple. Run through the pattern and fore every character get the index of it’s last occurence in the string. The index must always increase, otherwise return false.
    So in pseudocode:

    index = -1
    foreach c in pattern
        checkindex = string.lastIndexOf(c)
        if checkindex == -1                   //not found
            return false
        if checkindex < index
            return false
        if string.firstIndexOf(c) < index     //characters in the wrong order
            return false
        index = checkindex
    return true
    

    Edit: you could further improve the code by passing index as the starting index to the lastIndexOf method. Then you would’t have to compare checkindex with index and the algorithm would be faster.

    Updated: Fixed a bug in the algorithm. Additional condition added to consider the order of the letters in the pattern.

    • 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 in an interview. Although the interview was for dot
I was asked this question in a job interview. The interviewer and I disagreed
I was asked this question during an interview. They're both O(nlogn) and yet most
I just finished a phone interview. The interviewer asked me a question: Tell me
(I asked this question in another way , and got some interesting responses but
I asked this question before, Here however I think I presented the problem poorly,
I asked this question a while back but now I'm looking to implement an
I asked this question: Serial Port (rs232) in Mono for multiple platforms and this
I've asked this question to the forums on the Mootools website and one person
I originally asked this question on RefactorMyCode , but got no responses there... Basically

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.