Amazon Interview Question for Software Engineer / Developers






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

strtok(mystring, " ")

- Mat February 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What he meant was not to use any built-in functions.

- Anonymous February 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Scan the given string and check each character if it is any of the given punctuation.
Hash can be used to store punctuation characters for faster search.
OR we can consider ascii values of characters and use binary search.

- Anurag Singh February 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

hmm..

- chandransuraj February 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1) Use two pointers a and b.
2) Set both of them to the start of the text
3) While end is not reached, keep incrementing b until it hits a space/punctuation.
4) When it does hit a space or punctuation, set a = b and increment count by 1, and go back to step 3

The final value of count is our answer.
(Note that you might need to tweak it a little to support strings consequent punctuations)

- chandransuraj February 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you explain why you need two pointers, just keep in incrementing b , matching characters and incrementing count.

- Anonymous March 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I recently faced the same question.But the interviewer was not happy with the solution as given above.He wanted some CONTEXT-BASED solution !

- Anoni February 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What do you exactly mean by context-based?
Do you mean exact coding for the question asked

- Anonymous February 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No, not code, but by context-based he said that for one particular character,there can be a set of another characters who would be defined as 'punctuation marks' for that character and would have to be skipped while counting the no of words in the sentence.
For eg- say for A, set of punctuation marks is {x,y,z}, so now whenever we encounter **Ax,this means the word has ended there.

- Anonymous February 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

/*
* File: main.cpp
* Author: Aaron
*
* Created on February 9, 2011, 6:21 PM
*/

#include <cstdlib>

#include <vector>

#include <ctype.h>

#define MAX_COUNT 10240
using namespace std;


/*
*
*/

void count_words(char* data,int* count);


int main(int argc, char** argv) {

int times=100;
int n=100;
vector<float> u;


char l[]="my test ; z";


while(true)
{
printf("input:");
char input[MAX_COUNT]={};
fgets(input, MAX_COUNT ,stdin);
input[strlen(input)-1]=0;

if(
strcasecmp((input), "quit") ==0
)
{
break;
}
int c=0;
count_words(input,&c);
printf("count :%d\n", c);

}
return 0;
}


bool is_invalid_in_word(char c)
{
bool v= !(
( (c>='a') && (c<='z') )
||
( (c>='A') && (c<='Z') )
);
return v;
//in your case,just change this function
//as below:
/**

if(c=='x')
{
return true;
}
if(c=='y')
{
return true;
}
if(c=='z')
{
return true;
}
return false;
*/


}

void count_words(char* data,int* count)
{
if(data==NULL)
{
*count=0;
return ;
}

int len=strlen(data);
bool inWord=false;

for(int i=0;i<len;i++)
{
char c=data[i];
bool invalid=is_invalid_in_word(c)
;
if(inWord)
{
if(invalid)
{
//*count=*count+1;
inWord=false;
}else
{
//skip
}
}else
{
if(invalid)
{
//skip
}else
{
*count=*count+1;
inWord=true;
}
}


}
printf("str:%s len :%d\n",data, len);


}

- HertzLee February 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why cant we just count the number of white spaces/punctuations/end of string in the string to get the number of words?

- HipHop February 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

No. If you were to have the following string: "Hello World!!", you would end up counting three words due to the extra exclamation mark when there are only two words to begin with.

- Anonymous February 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No, counting the no of spaces +1 should give the no of wrds. Why do u think the exclamation will cause a problem? we r just counting the spaces

- Anonymous March 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

while (!str ++) {
if(*str == ' ')
word_count++'
}
return (word_count+1)

- Kashyapa February 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a solution in C++:

#include <iostream>

bool iswordchar(char c)
{
    return(c>='A' && c<='z');
}

int wordcount(char *p)
{
    int count = 0;
    int i = 0;
    int word = 0;

    while (p[i] != '\0')
    {
        while (p[i] != '\0' && !iswordchar(p[i])) {
            i++;
            word = 0;
        }
        while (p[i] != '\0' && iswordchar(p[i]) ) {
            i++;
            word = 1;
        }
        count += word;
    }
    return count;
}

int wordcount_main() {

    char *p = "oh, it is a  rainy day again!";
    std::cout << wordcount(p);
    return 0;
}

- maia February 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int WordCount(String str)
{
if(str == null) {return 0;}
str = str.Trim();
int wordCount = 0, index =0;
while (index < str.Length)
{
// check if the current character is part of a word
while (index < str.Length && !char.IsWhiteSpace(str[index]))
{
index++;
}

wordCount++;

// skip whitespace until next word
while (index < str.Length && char.IsWhiteSpace(str[index]))
{
index++;
}
}
return wordCount;
}

- Anonymous May 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{ {{{public static int WordCount(String str) { if(str == null) {return 0;} str = str.Trim(); int wordCount = 0, index =0; while (index < str.Length) { // check if the current character is part of a word while (index < str.Length && !char.IsWhiteSpace(str[index])) { index++; } wordCount++; // skip whitespace until next word while (index < str.Length && char.IsWhiteSpace(str[index])) { index++; } } return wordCount; } }}} - Dillon May 23, 2017 | 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