Groupon Interview Question for Senior Software Development Engineers


Country: India
Interview Type: Phone Interview




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

We can use stack....

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

#include<iostream.h>
#include<conio.h>

int main() {
char input[50]="";
int count=0;;
cout<<"\nEnter the string : ";
cin>>input;
char *p=input;
int i=0;
while(p[i]!='\0')
{
if(p[i+1]==p[i]) {
count++;
char *q=&p[i];
while(*(q+2)!='\0') {
*q=*(q+2);
q++;
}
*q='\0';
i=0;
}
else
i++;
}
cout<<"\nfinally we have : "<<input<<"\nMaximum couples : "<<count;
getch();
return 0;
}

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

#include<stdio.h>
#include<stack>
#include<string.h>
using namespace std;
int main(){
    char input[]="YRGGRRGGRY";
    stack<int> s;
    int count=0;
    int len = strlen(input);
   for(int i=0;i<len;i++){
           if(s.empty()){
                s.push(input[i]);                       
           }else if(s.top() == input[i]){
                 s.pop();
                 count++;      
           }else{
                 s.push(input[i]);
           }      
    }
    printf("counter is %d",count);
    
return 0;    
}

- uditmanit January 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int maxPairs(char[] A) {
        if(A==null) {
            return 0;
        }
        int count = 0;
        Stack<Character> st = new Stack<Character>();
        for (int i = 0; i < A.length; i++) {
            if(st.isEmpty()) {
                st.push(A[i]);
            } else if(st.peek()==A[i]) {
                count++;
                st.pop();
            } else {
                st.push(A[i]);
            }
        }
        return count;
    }

- xuzheng712 January 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

top = 0 ;
a[] = //the character array ...

for (i=1;i<size;i++)
{
if(a[top] == a[i])
{
top = top - 1;
count++;
}

else
{
top = top+1;
}
}

- dazer January 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This will not work as top can be -1 if the first two elements are same top will be -1. Correct me if I am wrong

- Srinath January 07, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

It can be done simply like this 0(n) but any one having more optimised solution than o(n) ???

I think with divide and conquer approach we can do it in o(log n ) timing . correct me if i am wrong ..


thanks..

- dazer January 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Minimum complexity is O(n) as you have to go through all the elements atleast once to do it.

- kr.neerav January 24, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I was asked this question in the Groupon Interview. I told him the solution using stack but he asked me to find a solution with O(1) space complexity, which I couldn't. does anyone know how to solve this without using stack or brute force?

- Shubhankar January 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if we use the same array as stack we only need one extra element to track the index of top element of stack. This will be an O(n) time and O(1) space complexity.
If this is not allowed then we can use brute force i.e. to scan the array n times and remove values if they occur simultaneously. This will work in O(n^2) time and will require O(1) space to store the last value viewed in the scan.
This problem is something similar to finding palindrome but that also takes O(n^2).

- kr.neerav January 24, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

We can use below approach to solve having O(n) time complexity , its also taking around constant space .

public static int getMaxCouple(char aa[])
{
char garbageChar=(char)-1;
int len=aa.length;
int prev=0;
int i=1;
int count=0;
while(i<len && prev>=0)
{
if(aa[prev]!=aa[i])
{
prev++;
char t=aa[prev];
aa[prev]=aa[i];
aa[i]=t;
i++;
}
else
{
if(i<len && aa[prev]==aa[i])
{
aa[i]=garbageChar;
i++;
}
aa[prev]=garbageChar;
count++;
prev--;
}
}

return count;
}

- braj March 03, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You can if you destroy the string/character array.

Keep two pointers, last and next.
Main loop looks like this:

If (ar[last] == ar[next]) 
{
   ar[next++] = '\0';
   ar[last] = '\0'; 
  while (last >= 0){
     if (ar[last] == '\0'
       last = next++;
     else if (ar[last] != '\0')
        break;
     else
        last--;
   }
}

- kl1 November 03, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry - forgot to add description, and above code has a bug. :)

if (ar[last] == '\0'

should be:
if (last == 0)

Of course, this algo depends on there not being \0 in the string.


if last == next, then set both to \0. increment next and walk last backwards till you find the next non \0 entry.
If last gets to 0 set last to next and increment next again.

Repeat while next < ar.length.

- kl1 November 03, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

In the above question,the number of pairs can be given as (n-1)/2 if n is odd and n/2 if n is even where n is the length of longest palindromic substring.
This can be done in O(1) space complexity by recursive approach.
int lps(char *seq, int i, int j)
{
// Base Case 1: If there is only 1 character
if (i == j)
return 1;

// Base Case 2: If there are only 2 characters and both are same
if (seq[i] == seq[j] && i + 1 == j)
return 2;

// If the first and last characters match
if (seq[i] == seq[j])
return lps (seq, i+1, j-1) + 2;

// If the first and last characters do not match
return max( lps(seq, i, j-1), lps(seq, i+1, j) );
}

- AnkitaJ February 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Won't work - you're not guaranteed to have a palindrome.

consider: ggrryybyg

- kl1 November 03, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The longest palindromic *substring*. In your example, this equals "ggrryy", which has length 6. 6/2 = 3, the correct answer. I personally don't think finding palindromic substrings is the best approach to this problem, but it certainly is a different (and valid) way to solve it.

- Anonymous February 16, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I tried to write following...kindly update in case of any gap/error.

void removeSequence(char **strOrg){
char *str = *strOrg;
int p = 0,q = 0;

while(*(str+p) && *(str+p+1)){
if(*(str+p) != *(str+p+1)){
*(str+q) = *(str+p);
q++;
}
p++;
}
}

- kb.arora February 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

time complexity: O(N)
space: O(1)

#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;

int main() {
	int arr[15] = {1,2,2,5,4,6,6,4,5,3,3,2,1,1,2};
	int lastInd = 0, compInd = 1;
	int numCpls = 0;
	for(int ind = 1; ind < 15; ind++) {
		if(compInd != ind)
			arr[compInd] = arr[ind];
		if(arr[lastInd] == arr[compInd]) {
			cout << arr[lastInd] <<endl;
			compInd = max(0,lastInd);
			lastInd = max(0,lastInd - 1);
			numCpls++;
		} else {
			compInd++;
			lastInd++;
		}
	}
	cout << "numCpls = "<< numCpls<<endl;

	return 0;
}

- manungnv April 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public enum Color {
	R,
	G,
	B
};
int NumberOfPairs(Color[] input) {
	int counter = 0;
	for (int top = 0, current = 1; current < input.Length;) {
		if (input[top] == input[current]) {
			//remove pair
			top--;
			current++;
			counter++;
		} else {
			top = current;
			current++;
		}
	}

	return counter;
}

- KiR May 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This won't work.
If the first 2 colors are same, top will be -1 causing index-outof-bound.

- duck August 16, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

def max_sequence(color_arr)
  found_pair = 0
  new_sequences = []
  (0..color_arr.count-1).each do |c|
    if color_arr[c] == color_arr[c+1]
      found_pair = 1
      end_of_arr = c+1 == color_arr.count ? [] : color_arr[c+2..color_arr.count-1]
      start_of_arr = c == 0 ? [] : color_arr[0..c-1]
      new_sequences << start_of_arr + end_of_arr
    end
  end

  if new_sequences.any?{ |s| !s.empty? }
    return new_sequences.map{ |s| max_sequence(s) }.sort.max + found_pair
  else
    return found_pair
  end
end

- Anonymous April 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 5 vote

We can use stack to solve this problem.
Keep adding elements on the stack till the top of the stack is different then the currently added element. If they are the same remove the top and increment counter. For your example.
1) Add Y, R, G to the stack, next element is G so remove G and increment counter = 1;
2) Stack is Y,R - next is also R so remove the top, counter = 2;
3) Stack is Y, - next is R so add it.
5) Stack is Y R - add G
6) Stack is Y R G - next is also G so remove G, counter = 3
7) Stack is Y R - next is also R so remove the top, counter = 4
8) Stack is Y - the last element is Y, counter = 5
9) Stack is empty.

- thelineofcode January 13, 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