## Student Interview Question for fresherss

• 0

Country: United States

Comment hidden because of low score. Click to expand.
1
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
}
ptr++
}
}
return true
}

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

Comment hidden because of low score. Click to expand.
0
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' ) )``````

Name:

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

### Books

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

### Videos

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.