Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

int numWords (char *string)  {
     int numWords = 0;
     int wordHit = 0;
     for (int i = 0; i < strlen(string); i++) {
          char ch = string[i];
          if (ch == ' ' && wordHit == 1) {
               numWords++;
               wordHit = 0;
          }
          if ((isaplha(ch) || isdigit(ch)) && wordHit == 0) {
               wordHit = 1;
          }
     }
     return numWords;
}

- Anonymous October 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This works, except if the last word doesn't have a space after it so you could add wordhit to numwords on the return

- brnr October 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

public static int Count(string str)
{
    int wc = 0;
    for (int i = 0; i < str.Length; i++)
    {
         if (str[i] != ' ')
         {
              wc++;
              while (str[++i] != ' ' && i != str.Length - 1);
         }
    }
    return wc;
}

- Good Stuff October 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

int noOfWords(String s){
if s == null return 0;
while(i<s.length)
{
ifc[i]!=' ' and i==0)
count++;
if(c[i]==' ' and c[i+1]!= ' ')
count++;
return count;
}

- kannan s January 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

private static int numOfWords(String str) {
char[ ] arr = str.toCharArray();
int count = 0;
if(arr[0] != ' ') {
count++;
}
for(int i=1; i<arr.length; i++) {
if(arr[i-1]==' ' && arr[i]!=' ') {
count++;
}
}
return count;
}

- Anon October 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Fails when str==null or is empty.

- Chris October 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int length(char *str){
int count=0;
while (*str !='\0'){
if(*str==' ' && *(str+1)==' '){
str++;
continue;
}else if(*str==' ') count++;
str++;
}
return count;
}

- coder October 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#python code
s = " the quick brown fox jumped  over the   fence  "

word_count = 0
previous_is_blank = True

for c in s:
    if c == ' ':
        previous_is_blank = True
    else:
        if (previous_is_blank):
            word_count += 1
        previous_is_blank = False

print word_count

- bluesky October 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

# include<stdio.h>
# include<conio.h>
# include<stdlib.h>
int find(char *s)
{
int flag=0,count=0;//when flag is made 1 then it means that there are characters in between two white spaces
while(*s!='\0')
{
if(*s==' ')
{
if(flag==1)
{
count++;
flag=0;
}
}//end when white spaces occur
else if(flag==0)//character other than alphabets
flag =1;
s++;
}//end of while loop
if(flag==1)
count++;//for the last word
return count;
}//end of function
int main()
{
char s[100];
int count;
printf("enter the string");
gets(s);
count=find(s);
printf("The number of words present in the string are %d",count);
getch();
return 0;
}//end of main function

- Anonymous October 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

one line code in C#

private static int CountWords(string s)
        {
            return s.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries).Count();
        }

- kaustubh October 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Using Split is a guaranteed way to not pass an interview.

- Timmy October 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Similar in Java

1. Trim the string (to remove empty spaces from beginning and at the end)
2. Split using white spaces as a delimiter
3. The length of the array returned in step 2.

- HLS October 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<string.h>
#include <conio.h>
int main()
{
    char ch[1000];
    printf("Please Enter the String\n");
    gets(ch);
    int i=0,k=0,j;
    int len=strlen(ch);
    while(ch[i]!='\0')
    {
       if(ch[i]==' '||ch[i]=='\t') {i++;}
        else{
                 while((ch[i++]!=' '&& i<len)&&(ch[i++]!='\t' && i<len));
                  k++;
                 }
    }    
    printf("The number of words is=%d", k);
    getch();
}

- noobProg October 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class WordCount {
	public static void main(String[] args) {
		String s = " the quick brown fox jumped  over the   fence  ";
		boolean flag = false;
		int wc = 0;
		for (Character c : s.toCharArray()) {
			if (c == ' ')
				flag = false;
			else if (flag == false && c != ' ') {
				wc++;
				flag = true;
			}
		}
		System.out.println("  " + wc);
	}
}

- Priyank October 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Is it OK that no one treats other whitespaces (tabs, CR, LF, etc) as non-whitespace or for interview purposes it's presumed that whitespaces are spaces only?

- Alex October 23, 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