## Amazon Interview Question for Applications Developers

Country: India
Interview Type: Phone Interview

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

This is a known problem and available in CareerCup book:

1. Count the number of spaces during the first scan of the string.
2. Parse the string again from the end and for each character:
»»If a space is encountered, store “%20”.
»»Else, store the character as it is in the newly shifted location.

``````void ReplaceFun(char[] str, int length)
{
int spaceCount = 0, newLength, i = 0;
for (i = 0; i < length; i++)
{
if (str[i] == ‘ ‘)
{
spaceCount++;
}
}
newLength = length + spaceCount * 2;
str[newLength] = ‘\0’;

for (i = length - 1; i >= 0; i--)
{
if (str[i] == ‘ ‘)
{
str[newLength - 1] = ‘0’;
str[newLength - 2] = ‘2’;
str[newLength - 3] = ‘%’;
newLength = newLength - 3;
}
else
{
str[newLength - 1] = str[i];
newLength = newLength - 1;
}
}
}``````

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

Approach is correct...
But I think you have not used the correct variables at correct places... :P

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

I think it should have been following instead:

``````str[newLength - 3] = ‘0’;
str[newLength - 2] = ‘2’;
str[newLength - 1] = ‘%’;``````

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

Anonymous, that will produce the thing backwards.

Look at:

``[...] [newlength-3][newlength-2][newlength-1][newlength] [...]``

If you put 0 on [newlength-3] it would turn into "02%"

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

``````#include<stdio.h>
#include<string.h>
int main()
{
char a,b;
int len,i,j,spacecount=0;
gets(a);
len=strlen(a);
//printf("%d",spacecount);
for(i=0;i<len;i++)
{
if(a[i]==' ')
{
spacecount++;
}
}

j=len-1;
len=len+2*spacecount;
b[len]='\0';
i=len-1;
while(i>=0&&j>=0)
{

if(a[j]==' ')
{
b[i--]='0';
b[i--]='2';
b[i--]='%';
j--;
}
else
{
b[i--]=a[j--];
}
}

puts(b);
return 0;
}``````

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

``````private void encode(char[] word)
{
char[] spaceArr = { '%', '2', '0' };

for (int i = 0; i < word.length; i++)
{
if (word[i] == ' ')
{
System.arraycopy(word, i + 1, word, i + spaceArr.length, getWordLength(word)-i);
System.arraycopy(spaceArr, 0, word, i, spaceArr.length);

}
}
}

private int getWordLength(char[] word)
{
for (int i = 0; i < word.length; i++)
{
if (word[i] == '\0')
{
return i - 1;
}
}
return 0;``````

}

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

PHP code for replacing.

``````function replace_space(\$line) {
for (\$i = 0; \$i < strlen(\$line); \$i++) {
if (\$line[\$i] == " ") {
for (\$j = strlen(\$line) - 1; \$j >= \$i; \$j--) {
\$line[\$j+2] = \$line[\$j];
}
\$line[\$i] = "%";
\$line[\$i+1] = "2";
\$line[\$i+2] = "0";
}
}
return \$line;
}``````

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.

### 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.