Amazon Interview Question for Software Engineer in Tests






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

void RemoveSpaces(char* src, int len)
{
   char* copyFrom = src, *copyTo = src;

   for(int i = 1; i < len; ++i )
   {
      if(src[i-1] == ' ' && src[i]==' ')
      {
         ++copyFrom;
      }
      else
      {
         *copyTo++ = *copyFrom++;
      }
   }

   *copyTo = 0;
}

int main(int argc, char** argv)
{
   char arr[] = "Test String    to be  displayed ";
   RemoveSpaces(arr, strlen(arr));

   cout << arr << endl;
   return 0;
}

- gevorgk March 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming " " to be replaced....

String a = "There are multiple spaces";
while (a.contains(" ")){
a=a.replace(" ", " ");

}
System.out.println(a);

}

- starmoon March 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Not efficient, there will be multiple copyings of string

- gevorgk March 10, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

String a = "There are          multiple                      spaces";
		while (a.contains("  ")){
		a=a.replace("  ", " ");
	
	}
	System.out.println(a);
	
	}

- Starmoon March 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is a version of a possible solution:

string str = "Test String to be displayed";
char *ptr = (char *)str.c_str();
int index = 0,w_start = 0,w_end = 0;

while(*ptr) {
w_end = w_start;
while(*ptr && *ptr != ' '){
ptr++;
w_end++;
}
while(w_start < w_end)
str[index++] = str[w_start++];
str[index++] = ' ';

while(*ptr && *ptr == ' '){
w_start++;
ptr++;
}
}
for(; index < str.size(); index++)
str[index] = '\0';

cout << "Final: " << str << endl;

- ZooZoo March 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This code is very simple.
Just compare every element with every other element. If they both are spaces( in my prog i did for dashes!) they replace with a single dash and decrement the count of i by one.
Hope it helps!

private static void MultipleSpaces(){
String str = "Test-String-----to-be--displayed--";
for(int i=0;i<str.length()-1;i++){
if(str.charAt(i)=='-' && str.charAt(i+1)=='-'){
i--;
str=str.replace("--","-");
}
}
System.out.println(str);
}

- Anonomyous March 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you are using extra space here,
str=str.replace("--","-");

- tetura March 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

First, using str.length() in loop is usually inefficient.
Second, every time you do str=str.replace("--","-"); it creates a new copy of string. so it uses extra space

- Jovi June 06, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using Perl...

$str=~ s/\s+/-/g;

- jp March 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Should not use Perl REGEX
2. Should not use any extra character string (char arr[size])

Hint - use two char pointers on the same string

- Roshan March 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<string.h>
int main()
{
char str[50];
int len,i=0,j,spaces=0;
clrscr();
printf("\n Enter the String\n");
gets(str);
len=strlen(str);
printf("\n Length of the String is %d\n",len);
for(j=0;j<=len;j++)
{
if(str[j]==' ')
spaces++;
else
{
if(spaces>0)
{
str[i]=' ';
i++;
}
str[i]=str[j];
i++;
spaces=0;
}
}
str[i]='\0';
printf("Result String %s",str);
}

- Rajesh Manem March 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>

int main()
{
char str[] = " This is my junk code";
char *p, *q;

p = q = str;
while (*p) {
while (*p == ' ' && *p != '\0')
p++;
if (*(p-1) == ' ' && *p != ' ')
*q++ = *(p -1);
*q++ = *p++;
}
*q = *p;
fprintf( stderr, "The value is :%s\n", str);
return 0;
}

- Anonymous March 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This code works in JAVA

String test = " hey how are you";
test = test.replaceAll("\\s+", " ");
System.out.println(test);

- Woggle March 31, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void replace(char *str)
{
    int i = 1; j = 1;        //notei and j = 1, not 0   
    char *cur = str;
    while(cur[i])
    {
        if(cur[i] == ' ')
        {
            if(cur[i-1] == ' ')        //first occurence
            {
                //ignore it
             }
        }
        else
        {
           cur[j++] = cur[i];
        }
}

- Dee April 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I forgot to add this last line before returning form the function:
cur[j] = '\0';

- Dee April 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

rplace(char * c,int l)
{
char *p,*q;
p=c;
q=c;
for(i=1;i<l;i++)
if(p[i-1]==' ' && p[i]==' ')
{
++p;
}
else
{
*q++=*q++;
}
}
*q='\0';
}

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

#define TrimCharacter '-'
char * Trim(char * inputString,int l)
{
    char *p,*q;
    q = p = inputString;
    while(*p){
        if((*p != TrimCharacter) || (*q != TrimCharacter)) {
            *q = *p;
            q++;
        }
        p++;
    }
    *q = 0;
    return inputString;
}

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

your code has bug..it trims all spaces

- aaa August 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="c++" line="1" title="CodeMonkey19636" class="run-this">#include<iostream>
#include<string>

using namespace std;

int main()
{
string s=" Hello, how r u???????";

int pos,i=0;


for(i=0;i<s.length();i++)
{
if(s[i]==' ')
{ if(i!=0)
i=i+1;
while(s[i]==' ')
{
s.erase(s.begin()+i);
}
}
}

cout<<s<<"\n";

return 0;

}</pre><pre title="CodeMonkey19636" input="yes">
</pre>

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

public class nr_LimitSpaceBetweenWordsInAStringToOne
{

/**
* @param args
*/
public static void main(String[] args)
{
String s = "Test String to be displayed ";
//String s = "AA B C D";
int count = 0;
for(int i=0;i<s.length();i++)
{
count = 0;
while(i<s.length() && s.charAt(i)==' ')
{
i++;
count++;
}

if(count > 1)
{
System.out.println("i= "+i+" count= "+count);
s=s.substring(0,i-count+1)+s.substring(i,s.length());
System.out.println(s);
i = 0;
}
}
System.out.println("");
System.out.println("Final String after removing all extra spaces is: ");
System.out.println(s);
}
}

- pal August 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In ruby we could just do 

s = "Test string   to be            displayed" 
s.gsub!(/\s+/,' ')
p s 
# =>"Test string to be displayed"

- CodePanda August 15, 2014 | 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