Amazon Interview Question for Software Engineer in Tests


Country: United States




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

#include <stdio.h>
#include <stdlib.h>

int main () {
char *s = "This is an example";
char r[100];

int i = 0;

while (*s) {
if (*s != ' ') {
r[i] = *s;
}
else {
r[i] = *s;
while (*(++s) && *s == ' ');
s--;
}
s++; i++;
}

r[i] = 0;

printf ("Result: %s\n", r);
}

- Nitin February 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The question is not clear ! the input and output looks the same.

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

I guess its - input ="This is an example"
And output = "This is an example"

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

i/p = "This_____is__an_example"
o/p="This_is_an_example".
HTML is removing extra spaces..

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

No, Example : Here__is_another____example_dude.
TotalSpaceCount = 8.
No of Words=5
SpacesbetweenEachWord=TotalSpaceCount/(NoOfWords-1)

Considering there is no space at the end of a sentence.
Output: "Here__is__another__example__dude."

- ariesgirl069 February 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No, Example : Here__is_another____example_dude.
TotalSpaceCount = 8.
No of Words=5
SpacesbetweenEachWord=TotalSpaceCount/(NoOfWords-1)

Considering there is no space at the end of a sentence.
Output: "Here__is__another__example__dude."

- ariesgirl069 February 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I'm guessing this question is asking you to modify an input string by removing multiple spaces. If so:

void remove_multiple_spaces(char* str) {
  // Check for null or empty string
  if(!str || !*str) {
    return;
  }

  // Maintain a 'read' pointer and a 'write' pointer.  The 'read' pointer
  // will advance past the write pointer when we hit consecutive spaces.
  char* read = str;
  char* write = str;
  
  while(*read != '\0') {
    // Copy from 'read' to 'write' and advance each pointer
    char c = *read++;
    *write++ = c;

    // If we encounter consecutive spaces, advance 'read'
    if(c == ' ') {
      while(*read == ' ') {
        ++read;
      }
    }
  }

  // Finally, add a trailing null char at the end of the string
  *write = '\0';
}

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

#include <stdio.h>
#include <stdlib.h>

int main () {
	char *s = "This  is        an                   example";
	char r[100];

	int i = 0;

	while (*s) {
		if (*s != ' ') {
			r[i] = *s;
		}
		else {
			r[i] = *s;
			while (*(++s) && *s == ' ');
			s--;
		}
		s++; i++;
	}

	r[i] = 0;

	printf ("Result: %s\n", r);
}

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

No, Example : Here__is_another____example_dude.
TotalSpaceCount = 8.
No of Words=5
SpacesbetweenEachWord=TotalSpaceCount/(NoOfWords-1)

Considering there is no space at the end of a sentence.
Output: "Here__is__another__example__dude."

- ariesgirl069 February 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

countspace = 0;
writehere = string;
for( char* i = string; *i; i++ )
{
	if( *i != ' ' )
		*writehere++ = *i;
		countspace = 0;
	else
		if( countspace == 0 )
			*writehere++ = *i;
		countspace++;
}
*writehere = 0;

- y2km11 February 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

hey ariesgirl069, here is the javacode
class TrimSpacesInString{
public static String trimString(String inputString){
if(inputString == null)
return null;

String inputStringT = inputString.trim();
StringBuffer result = new StringBuffer();

char temp;
int spaceCount = 0;
for(int i = 0; i < inputStringT.length(); i++){
temp = inputStringT.charAt(i);

if( temp != ' '){
if(spaceCount > 0)
result.append(' ');
result.append(temp);
spaceCount = 0;
}
else
spaceCount++;
}

return new String(result);
}

public static void main(String args[]){
System.out.println(trimString(" This is an example "));
return;
}
}

- viv March 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class EqualSpacesInWords {

	public static void main(String[] args) {
		String s1 = "This   is   an  example";
		String s2 = equalSpacesInWords( s1 );
		System.out.println( s2 );
	}

	private static String equalSpacesInWords( String s ) {
	  String formatted = "";
	  List<String> listStr = new ArrayList<String>();
	  String word = "";
	  char[] a = s.trim().toCharArray();
	  int countSpaces = 0;
	  boolean processWord = true;
	  
	  for( int i = 0; i < a.length; i++ ) {
	  	if( processWord ) {
		  	if( a[i] != ' ' ) {
		  		word += a[i];
		  		if( i == a.length - 1 ) {
		  			listStr.add( word );
		  		}
		  	}
		  	else {
		  		countSpaces += 1;
		  		processWord = false;
		  	}
	  	}
	  	else {
	  		if( a[i] == ' ' ) {
	  			countSpaces += 1;
	  		}
	  		else {
	  			listStr.add( word );
	  			word = "";
	  			word += a[i];
	  			processWord = true;
	  		}
	  	}
	  }
	  
	  int formattedSpaces = countSpaces / listStr.size();
	  while( !listStr.isEmpty() ) {
	  	formatted += listStr.remove(0);
	  	if( !listStr.isEmpty() ) {
		  	for( int i = 0; i < formattedSpaces; i++ ) {
		  		formatted += " ";
		  	}
	  	}
	  }
	  
	  return formatted;
  }
}

- howardkhl January 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public class CompressStringSpace {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "This is Example";
System.out.println(str);
String newStr = compressSpace(str);
System.out.println(newStr);
}

public static String compressSpace(String str){
StringBuffer buffer = new StringBuffer();
char[] charSet = str.toCharArray();
for(int i =0;i<str.length();i++){
if((str.charAt(i)!=' ')||((str.charAt(i) == ' ') && str.charAt(i)!=str.charAt(i+1)))
buffer.append(str.charAt(i));
}
return buffer.toString();
}
}

- Using StringBuffer in JAVA June 30, 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