Groupon Interview Question for SDE1s


Country: United States
Interview Type: Phone Interview




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

#include<iostream>
using namespace std;
void decode(string &s)
{
	int i=0,j=0;
	while(s[i]!='\0')
	{
		if(s[i]!='%')
		{
			s[j]=s[i];
			j++;
			i++;
		}
		else
		{
			if(s[i+1]=='2'&&s[i+2]=='0')
			{
				s[j]=' ';
				j++;
				i+=3;
			}
			else if(s[i+1]=='3'&&s[i+2]=='A')
			{
				s[j]='?';
				j++;
				i+=3;
			}
			else if(s[i+1]=='3'&&s[i+2]=='D')
			{
				s[j]='.';
				j++;
				i+=3;
			}
			else
			{
				s[j]=s[i];
				i++;
				j++;
			}
		}
	}
	s[j]='\0';
}

int main()
{
	string s;
	cout<<"Enter the string. \n";
	cin>>s;
	cout<<"Before="<<s<<"\n";
	decode(s);
	cout<<"After="<<s;
	return 0;	
}

- chaitanya July 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

s[i+1]=='2'&&s[i+2]=='0'.. Correct me If Im wrong but
In this line, you are not checking for null element, what if s[i+1] is the string termination character then it will crash.
Like for eg: "abcd%"

- Alice7 August 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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.

- Sharma July 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Sharma July 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sharma ji, you should not use newInputArray[] as the question is asking for in-place modification.

- Murali Mohan July 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the question is not about decode.
We should maintain a DS by which we can efficiently encode as well as decode.

- Debasis Jana July 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think its not abut decode.
We should maintain a DS by which we can do encode and decode efficiently.

- Debasis Jana July 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Joey July 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Anonymous July 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

}

- Nelson July 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Anonymous July 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

}

- Mukilan August 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

        }

- Richa August 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Jeff August 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Anonymous March 13, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- root March 22, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

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.

- Murali Mohan July 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is good why people down vated?

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

Yeah, this is good!

- Anonymous August 02, 2013 | Flag


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