Groupon Interview Question
SDE1sCountry: United States
Interview Type: Phone Interview
public class decodeURL {
public static void main(String[] args) {
String input = "kitten%3Kpic.jpg";
char[] lastChar = {'0','A','D'};
char[] replaceChar = {' ','?','.'};
int foundpos = -1;
for(int i = 0 ; i < input.length() ; i++) {
if(input.charAt(i) == '%') {
int pos = 0;
while(pos < 3) {
if(lastChar[pos] == input.charAt(i+2)) {
foundpos = pos;
break;
}
++pos;
}
}
}
if(foundpos > -1)
input = formatString(input,lastChar[foundpos],replaceChar[foundpos]);
System.out.println("output = " + input);
}
private static String formatString(String input, char last, char newChar) {
if(last == '0')
return input.replaceAll("%2"+last, Character.toString(newChar));
else
return input.replaceAll("%3"+last, Character.toString(newChar));
}
}
Please make comment if solution is not feasible.
char[] newInput = "kitten%3Dpic.jpg".toCharArray();
char[] newInputArray = new char[newInput.length];
int j = 0;
for(int i = 0 ; i < newInput.length ; i++) {
boolean found = false;
if(newInput[i] == '%' && newInput[i+1] == '2' && newInput[i+2] == '0') {
newInput[i] = ' ';
i = i+2;
found = true;
}else if(newInput[i] == '%' && newInput[i+1] == '3' && newInput[i+2] == 'A') {
newInput[i] = '?';
i = i+2;
found = true;
} else if(newInput[i] == '%' && newInput[i+1] == '3' && newInput[i+2] == 'D') {
newInput[i] = '.';
i = i+2;
found = true;
}
if(found)
newInputArray[j] = newInput[i-2];
else
newInputArray[j] = newInput[i];
++j;
}
for(int i = 0 ; i < newInputArray.length ; i++) {
System.out.print(newInputArray[i]);
}
Please comment.
char * DecodeURL(char *a,int n)
{
int i,j;
for(i=0,j=0;i<n && j<n; i++,j++)
{
if(a[i]=='%')
{
if(a[i+1]=='2' &&a[i+2]=='0')
a[i]=' ';
if(a[i+1]=='3' && a[i+2]=='A')
a[i]='?';
if(a[i+1]=='3' && a[i+2]=='D')
a[i]=':';
j=j+2;
}
else
{
a[i]=a[j];
}
}
a[i]='\0';
return *a;
}
#include<stdio.h>
int main()
{
char s[]="kitten%20.jpg";
int n=sizeof(s)-1;
DecodeURL(s,n);
printf("%s",s);
}
sites.google.com/site/csharpofjameschen/home/regular-expression/replace-patterns
while( i < len)
{
if (sb[i] == '%' && i < len - 2)
{
if (sb[i + 1] == '2' && sb[i + 2] == '0')
{
sb[j++] = ' ';
i += 3;
}
else if (sb[i + 1] == '3' && sb[i + 2] == 'A')
{
sb[j++] = '?';
i += 3;
}
else if (sb[i + 1] == '3' && sb[i + 2] == 'D')
{
sb[j++] = ':';
i += 3;
}
else
{
sb[j++] = sb[i++];
}
}
else
{
sb[j++] = sb[i++];
}
}
Here is using some not 100% efficient c# code. Using the if else statement should be the most efficient way I just did not liked the code duplication and this way is more scalable for new codes to encode.
void DecodeURL(string str)
{
if(str == null)
{
throw new ArgumentNullException("str");
}
Dictionary<char[], char> map = new Dictionary<char[], char>()
{
{ "%20", ' ' },
{ "%3A", '?' },
{ "%3D", ':' }
};
int strLength = str.Length;
int cRead = 0;
int cWrite = 0;
while(cRead < strLength && str[0] != '\0')
{
if(str[cRead = '%' && cRead < strLegnth - 3)
{
char[] next3 = new char[] { str[cRead], str[cRead + 1], str[cRead + 2] };
if(map.TryToGet(next3, out str[cWrite]))
{
cRead += 3;
cWrite++;
continue;
}
}
str[cWrite] = str[cRead];
cWrite++;
cRead++;
}
str[cWrite] = '\0';
}
Here is the efficient one but I would never code this in real life as it is very specific to those codes only and it is hard to understand that is for those codes.
void DecodeURL(string str)
{
if(str == null)
{
throw new ArgumentNullException("str");
}
int strLength = str.Length;
int cRead = 0;
int cWrite = 0;
while(cRead < strLength && str[0] != '\0')
{
if(str[cRead] == '%' && cRead < strLegnth - 3)
{
if( str[cRead + 1] == '2' && str[cRead + 2] == '0')
{
str[cWrite] = ' ';
cRead += 3;
cWrite++;
continue;
}
else if(str[cRead + 1] == '3')
{
if(str[cRead + 2] == 'A')
{
str[cWrite] = '?';
cRead += 3;
cWrite++;
continue;
}
else if(str[cRead + 2] == 'D')
{
str[cWrite] = ':';
cRead += 3;
cWrite++;
continue;
}
}
}
str[cWrite] = str[cRead];
cWrite++;
cRead++;
}
str[cWrite] = '\0';
}
}
public class InterviewProgram {
public static String decodeString(String input) {
for (int i = 0; i < input.length() - 2; i++) {
if (input.charAt(i) == '%') {
if (input.charAt(i + 1) == '3' && input.charAt(i + 2) == 'A') {
//replace with '?'
input = input.substring(0, i) + '?' + input.substring(i + 3);
}
if (input.charAt(i + 1) == '3' && input.charAt(i + 2) == 'D') {
//replace with ':'
input = input.substring(0, i) + ':' + input.substring(i + 3);
}
if (input.charAt(i + 1) == '2' && input.charAt(i + 2) == '0') {
//replace with ' '
input = input.substring(0, i) + ' ' + input.substring(i + 3);
}
}
}
return input;
}
public static void main(String[] args) {
String exampleInput = "kitten%20pic.jpg";
exampleInput = decodeString(exampleInput);
//"kitten pic.jpg"
System.out.println(exampleInput);
}
}
The solution is
oid DecodeURL(char *buffer)
{
char *ptr = buffer;
char *ptr1 = buffer;
while (*ptr != '\0')
{
if (*ptr == '%')
{
if (*(ptr+1) == '2' && *(ptr+2) == '0')
{
*ptr1 = ' ';
*ptr1++;
ptr += 3;
}
else if (*(ptr+1) == '3' && *(ptr+2) == 'A')
{
*ptr1 = '?';
*ptr1++;
ptr += 3;
}
else if (*(ptr+1) == '3' && *(ptr+2) == 'D')
{
*ptr1 = ':';
*ptr1++;
ptr += 3;
}
else
{
*ptr1 = *ptr;
ptr++;
ptr1++;
}
}
else
{
*ptr1 = *ptr;
ptr++;
ptr1++;
}
}
*ptr1 = '\0';
}
string DecodeURL(string str)
{
char[] strArray = str.ToCharArray();
string charsToBeReplaced;
for (int i = 0; i < strArray.Length; i++)
{
if (strArray[i] == '%')
{
charsToBeReplaced = str.Substring(i, 3);
switch (charsToBeReplaced)
{
case ("%20"):
{
strArray[i] = ' ';
strArray[i + 1] = ' ';
strArray[i + 2] = ' ';
break;
}
case ("%3A"):
{
strArray[i] = '?';
strArray[i + 1] = ' ';
strArray[i + 2] = ' ';
break;
}
case ("%3D"):
{
strArray[i] = '.';
strArray[i + 1] = ' ';
strArray[i + 2] = ' ';
break;
}
}
}
}
int len = strArray.Length;
for (int i = 0; i < len; i++)
{
if (strArray[i + 1] == ' ' && strArray[i + 2] == ' ')
{
for (int j = i + 3, k=i+1; j < strArray.Length; j++, k++)
{
strArray[k] = strArray[j];
}
len -= 2;
}
}
return new string(strArray, 0, len);
}
class Kitten
{
static String case1= "%20";
static String case2= "%3A";
static String case3= "%3D";
public static void main (String[] args){
String jeff= args[0].toString();
DecodeURL(jeff);
}
static void DecodeURL(String str)
{
for(int i =0; i<str.length()-1 ; i++)
{
if(str.substring(i, i+3).compareTo(case1)==0 )
{
System.out.println(str.substring(0, i) + " " +str.substring(i+3) );
break;
}
if(str.substring(i, i+3).compareTo(case2)==0 )
{
System.out.println(str.substring(0, i) + "?" +str.substring(i+3) );
break;
}
if(str.substring(i, i+3).compareTo(case3)==0 )
{
System.out.println(str.substring(0, i) + ":" +str.substring(i+3) );
break;
}
}
}
}
void modifyInPlace(char* url, char elem, int p, int len) {
url[p++] = elem;
while (p+2 < len) {
url[p] = url[p+2];
p++;
}
url[p] = '\0';
}
void DecodeURL(string& str) {
int len = str.length();
if (len == 0)
return;
int p = 0;
char* url = (char*) malloc((len + 1)*sizeof(char));
str.copy(url, len, 0);
while (p < len) {
if (url[p] == '%') {
if (p+1 < len && p+2 < len) {
if (url[p+1] == '2' && url[p+2] == '0') {
modifyInPlace(url, ' ', p, len);
} else if (url[p+1] == '3'){
if (url[p+2] == 'A') {
modifyInPlace(url, '?', p, len);
} else if (url[p+2] == 'D') {
modifyInPlace(url, ':', p, len);
}
}
} else return;
}
p++;
}
str.assign(url);
free(url);
url = NULL;
}
public static String decodeUrl(String st) {
char[] str = st.toCharArray();
int end = str.length - 1;
for (int i = 0; i <= end; i++) {
if (str[i] == '%') {
String s = "";
for (int j = i; j < i + 3 && j <= end; j++)
s += str[j];
boolean shift = false;
switch (s) {
case "%20":
str[i] = ' ';
shift = true;
break;
case "%3A":
str[i] = '?';
shift = true;
break;
case "%3D":
str[i] = ':';
shift = true;
break;
}
if(shift) {
for (int j = i + 1, k = i + 3; k <= end; k++, j++)
str[j] = str[k];
end = end - 2;
}
}
}
return new String(str, 0, end + 1);
}
Maintain 2 pointers i and j.
0. Start at the beginning of the string.
1. Copy the character at j to the character at i and advance both i and j one step to the right.
2. If the character at index i is a '%', advance j by 3 characters to the right.
3. If the substring between i and j = '%3A', replace '%' with a '?' at the ith position and advance i by 1 position to the right. Otherwise do nothing.
4. Repeat steps 1 & 2 till j reaches the end of the string.
5. Terminate the string at position i.
- chaitanya July 03, 2013