Amazon Interview Question
Java DevelopersCountry: United States
// 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';
}
}
#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;
}
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'));
}}
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")
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")
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")
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:
- prudent_programmer March 05, 2018