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;
        }
     }
}

- Dilbert Einstein May 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- coding.arya May 13, 2013 | Flag
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] = ‘%’;

- Anonymous May 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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%"

- bunnybare May 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<string.h>
int main()
{
char a[50],b[50];
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;
}

- kaushal yadav May 13, 2013 | Flag Reply
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;

}

- Daya May 14, 2013 | Flag Reply
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;
}

- Cool GUY - PHP Code June 18, 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