Amazon Interview Question for Java Developers


Country: United States




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

The easiest way would be to use the library function replace() function but since the question specifically asks not to use any library functions, the alternative way is to use three pointers, prev, curr, and next. If the three pointers match any one of the special characters below, then use the pop() function to modify the string in place.
If you're using python, another quick way to do this would be to use the python's slicing to find the strings and replace it with the appropriate character to be replaced. I present both solutions below.

Solution:

def decodeString(encodedStr):
    curr = 0
    encodedStr = list(encodedStr)
    specialCharacters = \
    {
        '%20':' ',
        '%3A':'?',
        '%3D':':'
    }
    while curr < len(encodedStr) - 2:
        specialCharacter = ''.join(encodedStr[curr:curr+3])
        if specialCharacter in specialCharacters:
            encodedStr.pop(curr+2)
            encodedStr.pop(curr+1)
            # Replace by the appropriate decoded character
            encodedStr[curr] = specialCharacters[specialCharacter]
        else:
            curr += 1
    return ''.join(encodedStr)

def decodeUsingThreePointers(encodedStr):
    prev, curr, next = 0, 1, 2
    encodedStr = list(encodedStr)
    while prev < len(encodedStr) - 2:
        if encodedStr[prev] == '%' and encodedStr[curr] == '2' and encodedStr[next] == '0':
            encodedStr.pop(next)
            encodedStr.pop(curr)
            encodedStr[prev] = ' '
        elif encodedStr[prev] == '%' and encodedStr[curr] == '3' and encodedStr[next] == 'A':
            encodedStr.pop(next)
            encodedStr.pop(curr)
            encodedStr[prev] = '?'
        elif encodedStr[prev] == '%' and encodedStr[curr] == '3' and encodedStr[next] == 'D':
            encodedStr.pop(next)
            encodedStr.pop(curr)
            encodedStr[prev] = ':'
        else:
            prev += 1
            curr += 1
            next += 1
    return ''.join(encodedStr)

print(decodeUsingThreePointers('kitten%20pic.jpg')) # kitten pic.jpg
print(decodeUsingThreePointers('dog%3Aimg.jpg')) #dog?img.jpg
print(decodeUsingThreePointers('lion%3Dbook.txt')) #lion:book.txt
print(decodeString('kitten%20pic.jpg')) #kitten pic.jpg
print(decodeString('dog%3Aimg.jpg')) #dog?img.jpg
print(decodeString('lion%3Dbook.txt')) #lion:book.txt

- prudent_programmer March 05, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// JDK 9 only
private static final Map<String, Character> SPECIAL_CHARS = Map.of("20", ' ', "3A", '?', "3D", ':');

public static void decode(char[] str) {
    int readIdx = 0, writeIdx = 0;

    while (readIdx < str.length) {
        char replacement = str[readIdx];

        if (replacement == '%' && readIdx + 2 < str.length) {
            Character c = SPECIAL_CHARS.get("" + str[readIdx + 1] + str[readIdx + 2]);
            if (c != null) {
                replacement = c;
                readIdx += 2;
            }
        }

        str[writeIdx++] = replacement;
        readIdx++;
    }

    while (writeIdx < str.length) {
        str[writeIdx++] = '\u0000';
    }
}

- jgriesser March 05, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#define STR_LEN 16

void decode(char str[STR_LEN])
{
    int i, j;
    
    j = 0;
    while('%' != str[j])
        j++;

    i = j;
    j += 2;

    if ('0' == str[j])
        str[i] = ' ';
    if ('A' == str[j])
        str[i] = '?';
    if ('D' == str[j])
        str[i] = ':';

    while(STR_LEN > j) {
        i++;
        j++;
        str[i] = str[j];
    }

    str[++i] = '\0';

    printf("modified string : %s\n", str);
    return;
}


int main()
{
    char str[STR_LEN] = {'k','i','t','t','e','n','%','3','D','p','i','c','.','j','p','g'};

    decode(str);

    return 0;
}

- rajeshprasad05 March 06, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In Javascript

{{ function strMap(ori, rep){
this.original = ori;
this.replacement = rep;
}

let strMapArr = [];
strMapArr.push(new strMap("%20"," "));
strMapArr.push(new strMap("%3A","?"));
strMapArr.push(new strMap("%3D",":"));

function replaceSpecail(str){
let found = false;
let key="";
let result="";
for(i=0;i<str.length;i++){
if (str[i] === '%') {
key= str[i]+str[i+1]+str[i+2];
for(n=0;n<strMapArr.length;n++){
if (key === strMapArr[n].original){
result += strMapArr[n].replacement;
i = i + 2;
}
}
key="";
} else
result += str[i];
}
return result;
}

console.log(replaceSpecail('kitten%20pic.jpg'));
console.log(replaceSpecail('dog%3Aimg.jpg'));
console.log(replaceSpecail('lion%3Dbook.txt'));
}}

- Manoj Kumar March 08, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def encodeDecode(str):

charDict = {
'%20': ' ',
'%3A': '?',
'%3D': ':'
}
s1 = str
length = len(s1)
ds = ""
i =0

while i < length:
if s1[i] == '%' and i < length:
popString = s1[i]+s1[i+1] + s1[i+2]
for key in charDict:
if key == popString:
popString = charDict[key]
ds = ds+popString
i += 3
elif i > length:
break
else:
ds += s1[i]
i += 1
print ds

encodeDecode("abc%20pic.jpg%3Aabc%3Dvv")
encodeDecode("xy%3Apic.jpg%20abc%3Dhh")

- SG March 11, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def encodeDecode(str):

    charDict = {
        '%20': ' ',
        '%3A': '?',
        '%3D': ':'
    }
    s1 = str
    length = len(s1)
    ds = ""
    i =0

    while i < length:
        if s1[i] == '%' and i < length:
            popString = s1[i]+s1[i+1] + s1[i+2]
            for key in charDict:
                if key == popString:
                    popString = charDict[key]
                    ds = ds+popString
            i += 3
        elif i > length:
            break
        else:
            ds += s1[i]
            i += 1
    print ds

encodeDecode("abc%20pic.jpg%3Aabc%3Dvv")
encodeDecode("xy%3Apic.jpg%20abc%3Dhh")

- SG March 11, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def encodeDecode(str):

    charDict = {
        '%20': ' ',
        '%3A': '?',
        '%3D': ':'
    }
    s1 = str
    length = len(s1)
    ds = ""
    i =0

    while i < length:
        if s1[i] == '%' and i < length:
            popString = s1[i]+s1[i+1] + s1[i+2]
            for key in charDict:
                if key == popString:
                    popString = charDict[key]
                    ds = ds+popString
            i += 3
        elif i > length:
            break
        else:
            ds += s1[i]
            i += 1
    print ds

encodeDecode("abc%20pic.jpg%3Aabc%3Dvv")
encodeDecode("xy%3Apic.jpg%20abc%3Dhh")

- SG March 11, 2018 | 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