Amazon Interview Question for Software Engineer in Tests


Country: United States
Interview Type: Phone Interview




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

int main()
{
  char str[] = "Sea!tle is a nice place. Work Hard! have Fun, make HIStory!";
  int i = 0;
  bool first_char = true;
  while (str[i])
  {
    if (isalpha(str[i]))
    {
        if (first_char)
        {
            printf("%c", toupper(str[i]));
            first_char = false;
        }
        else
            printf("%c", tolower(str[i]));        
    }
    else if (str[i] == '!' && isalpha(str[i+1]))
    {
        printf("%c", tolower(str[i+1]));
    }
    else
    {
        if (str[i] == ' ')
        {
            if (!first_char)
                printf("%c", ' ');  
        }
        else
        {
            printf("%c\n", '.');    
            first_char = true;  
        }        
    }    
      
    i++;
  }

  return 0;
}

- Santosh April 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

above code is workable

- Santosh April 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

I don't think above answer is correct. I think question is to of pattern maching. Sea!tle should be found in dictionary with best matching.

I would use Tries to solve the problem.

- Mukesh Dua April 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Interesting proposal using Tries.
How would you match Sea!tle with Seattle with this approach?

- puneet.sohi April 21, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

In Tries, I would first search S, then Se, then Sea. Finally once we are sea, i'll search in all child nodes of it.

I think same approach would be implemented in Google also while searching the element.

- Mukesh Dua April 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I used to like Careercup but after seeing the above comment I won't come back here again.

- Anonymous April 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Instead of blaming comment, i would recommend to provide solution.

- Mukesh Dua April 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

Its a string manipulation question.
Go through your string,
if you find !, replace with t unless it is at the end of a word(Like in Hard!)
if you find ., goto new line
Similarly, you can make other conversions..

- puneet.sohi April 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void process(char s[])
{
     int i=0,flag=1;
     while(s[i]!='\0')
     {
         if(flag==1 && s[i]==' ')
         {
            i++;
            continue;
         }
         if(flag==1)
            toupper(&s[i]);
         else
            tolower(&s[i]);
         putchar(s[i]);
         if(flag==1)
            flag=0;
         if(s[i]=='.')
         {
            putchar('\n');
            flag=1;
         }
         i++;
     }
     putchar('\n');
}

- Nitin April 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

in java

public static String prettyPrint(String sentence) {
		// split every word
		ArrayList<String> split = new ArrayList<String>(Arrays.asList(sentence.split("\\s")));
		
		String output = "";
		boolean cap = true;
		boolean newline = false;
		
		// go through each word
		for (int i = 0; i < split.size(); i++) {
			String each = split.get(i);
			int len = each.length();
			
			// check if the last character is a '.', ',', or '!'
			switch (each.charAt(len - 1)) {
			case '.':
			case ',':
			case '!':
				newline = true;
				each = each.substring(0, len - 1) + ".";
			}
			
			// if you found an '!' replace it with 't'
			each = each.replace("!", "t");
			
			// first word of the sentence must be capitalized
			if (cap) {
				output += each.substring(0, 1).toUpperCase() + each.substring(1);
				cap = false;
			} else {
				// rest of the words are all in lowercase with a prepending space
				output += " " + each.toLowerCase();
			}
			
			// add a new line and make sure to mark the caplitalized flag as true
			if (newline) {
				output += '\n';
				
				cap = true;
				newline = false;
			}
			
		}
		
		return output;
	}

- Anonymous April 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

USing strtok :

#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="- This, a sample string.";
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str," ,.!");
  while (pch != NULL)
  {
    toupper(pch[0]);
    printf ("%s\n",pch);
    pch = strtok (NULL, " ,.-");
  }
  return 0;
}

- iwanna April 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I could do some similar in Python,

a = "Sea!tle is a nice place. Work Hard! have Fun, make History!"
b = a.replace("!","t",1)
c = b.replace(", ",".\n")
d = c.replace("! ",".\n")
e = d.replace(". ",".\n")
f = e.replace("!",".")
for line in f.splitlines():
    print(line.capitalize())

- Jareen November 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Not the prettiest solution but ruby solution works

=begin
  Give this input: Sea!tle is a nice place. Work Hard! have Fun, make HIStory!
  display this output using any language
  Seattle is a nice place. 
  Work hard. 
  Have fun. 
  Make history.
=end

def format_statement
  str = 'Sea!tle is a nice place. Work Hard! have Fun, make HIStory!'
  str = str.split(" ")
  str[0] = str[0].gsub('!', 't')
  first, i, first_part = str[0..4], 5, first_part = true
  puts first.join(" ")
  until i == str.size do
    if first_part == true
      str[i] = str[i].capitalize
      first_part = false
    elsif
      str[i] = str[i].downcase
      first_part = true
    end
  i +=1
  end
  str = str.join(" ").gsub(",", ".").gsub("!", '.').split(" ")
  puts str[5..6].join(" ") + "\n" + str[7..8].join(" ") + "\n" + str[9..10].join(" ")
end

format_statement

- VinceBarresi August 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void seattleLineParser(String input){
    def lines = input.split("[!.,][\\s+]");
    for (line in lines){
        def newLine = ""
        def first = true
        for (char_ in line.replace("!", "").toCharArray()){
            if (first) { first = false; newLine += char_.toUpperCase() }
            else newLine += char_.toLowerCase()
        }
        println "$newLine."
    }
}

seattleLineParser("Sea!tle is a nice place. Work Hard! have Fun, make HIStory!")

- crypthone May 29, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class AmazonSplitString {

	public static void main(String[] args) {
		String s="Sea!tle is a nice place. Work Hard! have Fun, make HIStory!";
		s=s.replaceFirst("!", "t");
		modify(s);
		

	}
	public static void modify(String s)
	{
		String[] str=s.split("\\.|\\,|\\!");
		
		
		for(int i=0;i<str.length;i++)
		{
			String string=str[i];
			string=string.trim();
			String result="";
			for(int j=0;j<string.length();j++)
			{
				char c=' ';
				if(j==0)
				{
				c=string.charAt(j);
				c=Character.toUpperCase(c);
				result+=c;
				}
				else
				{
					c=string.charAt(j);
					c=Character.toLowerCase(c);
					result+=c;
				}
				
				
			}
			System.out.print(result+".");
			System.out.println();
			

			
			}
			
		
			
		}
	}

- Anonymous June 25, 2020 | 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