Interview Question


Country: United States




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

grep 'string1\|string2\|string3' filename

- RossM June 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

I would like to clarify, you can't use inbuilt commands. You have to actually implement the grep function, also the strings can be part of words in the file.

- govind June 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

correction not sentences but lines

- govind June 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Treating it like an intersection problem just complicates it. If a sentence contains any of the words... why test it with another word...?
The idea would be to linearly traverse all sentences.
Test each sentence for the presence of one word. If you find it.. print it.. if you dont find it, test the same sentence again with the 2nd word and so on..

- Lokesh June 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can this be treated as a set intersection problem?
Create sets of lines which contains string1. Similarly for string2 and string3.
The solution would be to union of all the three sets.

The question then would be how to create sets for these strings? I mean, should it be line number or the entire line itself that should go in the sets?

- Learner June 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this problem is solved by using strstr in built c function...

first intialize the variable str1,atr2,str3 with the given input string
and take one buf[maxsize]; int i=0;
file* fp= fopen(argv[1],"r");
while(ch!=null){
ch=fread(fp,1,1);
buff[i]=ch;
if(ch=='\n'){if((strstr(buf,str1))!==null) printf("%s",buf);
if((strstr(buf,str2))!==null) printf("%s",buf);
if((strstr(buf,str1))!==null) printf("%s",buf);

i=0;
}
else
{ i++
}

}

- roshan June 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can use Hash code for the above problem... For the 3 strings we can calculate Hash code and we can store it.... then we can start reading the file, and we will calculate the Hash code for each word and if it matches with any 3 string hash code, we will print the entire line and then the same process is repeated for the next line and so on..

- vimal June 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

would it be better to just use normal string comparison using strcmp rather than creating a hash code and again matching the hash code??

- Anant June 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

First off.. lets just use a fast string compare algorithm... we can easily use Knuth Morris Pratt.
Start string matching with the first word.
For each sentence first just try out the entire sentence for the first word. If there is a match, print the sentence and go to the next. If there is no match, repeat the search on the sentence with the next word and so on...
That way.. if we get a match.. we print the sentence.. and we dont need to test the sentence again with another word because it is redundant.

- Lokesh June 19, 2012 | Flag Reply


Add a Comment
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.

Learn More

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.

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