Student Interview Question for fresherss

Country: United States

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

 * Function to find if the characters of the sample string are
 * in the same order in the text string. They need not be
 * consecutive.
 * E.g., TextString: Redmond, Washington
 *       SampleString: Rde Waitn

function compare (text, sample) {
  var ptr = 0
  var i = 0
  for (; i < sample.length; ++i) {
    while (text[ptr] !== sample[i]) {
      if (ptr >=  text.length) {
        return false
  return true

console.log(compare("Redmond, Washington", "Rdd Waitn"))

- srterpe December 04, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
of 0 vote

Think in terms of automata. Every letter in the sample string is a state, which gets activated when a letter matches, and then it waits for the next state, which is the next letter.
Thus, for a sample string -> s[i] where s[i] denotes i'th letter, s[0] is the first letter, we have this automata in place ( axiomatic ) with s[-1] is the last letter :

( anything not s[0] ) * -> s[0] -> ( anything not s[1] ) * -> s[1] .... -> s[-1] -> end

We are confused about whether or not s[0] should be the first letter and s[-1] should be the last letter of the text string - but those are easy to adjust. Thus, the automata can be coded as (in ZoomBA):

def recognize( pattern_string , text_string ){
  cur_pattern_index = 0 
  for ( char : text_string ){
     continue ( char != pattern_string[ cur_pattern_index ] )
     cur_pattern_index += 1 // matched, so move to next pattern letter 
  return cur_pattern_index == size( pattern_string ) 
println ( recognize('Rdd Waitn' , 'Redmond, Washington' ) )
println ( recognize('Rdd Waixtn' , 'Redmond, Washington' ) )

- NoOne December 04, 2016 | 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