Amazon Interview Question for Software Engineer / Developers






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

<pre lang="c++" line="1" title="CodeMonkey79468" class="run-this">It depends on what does one mean by "word" here? If characters between spaces and TABs and EOL, then code maybe like this:

int main()
{
ifstream in_file("c:\\test.txt");

int words_cnt(0);
while(in_file.good())
{
string tmp_str;
in_file >> tmp_str;
++words_cnt;
}

cout << "Words in file: " << words_cnt << "\n";

return 0;
}
</pre><pre title="CodeMonkey79468" input="yes">
</pre>

- Anonymous November 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Any reason of declaring tmp_str inside the loop as i guess it will create a new tmp_str variable in each loop iteration?

- JoshMachine November 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Right, no reason at all, ctor/dtor will run each step of cycle.

- Anonymous November 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Maintain a set of TAB SPACE EOF \O and \n
for every character you read from the file check if it belongs to this set. If it does then check if the previous element belongs to this set as well, if not then increment the count else do nothing

eg: I am a Student.
No. of words = 4 {space(1) space(4) space(6) EOF(15)} Where the number in the bracket represents the position in the string

I am a Student.
No. of words = 4 {Space(1) Space(4) Space(5) Space(7) EOF(16)} The space at the 5th position is discarded as it is a successor of another character from the set mentioned above so we don't count then

- ketz November 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Formatted version

Maintain a set of TAB SPACE EOF \O and \n
for every character you read from the file check if it belongs to this set. If it does then check if the previous element belongs to this set as well, if not then increment the count else do nothing

eg:

I am a Student.

No. of words = 4 {space(1) space(4) space(6) EOF(15)} Where the number in the bracket represents the position in the string

I  am a Student.

No. of words = 4 {Space(1) Space(4) Space(5) Space(7) EOF(16)} The space at the 5th position is discarded as it is a successor of another character from the set mentioned above so we don't count then

- Ketz November 11, 2010 | 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