Groupon Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

In ruby space is the default option for both split and reverse.

So, The one liner is

string.split.reverse.join

- Uday February 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice.

- sean February 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char sen[50];
char *array[40];
int loop;
clrscr();
printf("\nEnter The Date:");
gets(sen);
array[0]=(char *) strtok(sen," ");
if(array[0]==NULL)
exit(0);
for(loop=1;loop<50;loop++)
{
array[loop]=(char *) strtok(NULL," ");
if(array[loop]==NULL)
break;}
printf("The Reversed form of the given date is:");
for(--loop;loop>=0;loop--)
printf("%s ",array[loop]);
getch();
}

- Balaji.M PACET December 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

here am using date ... u can use string also like " hai how are u?"
it gives output as "u? are how hai"

- Balaji.M PACET December 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

then leave a space b/w u and ? ......

- Anonymous January 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

public class ReverseString{

public static void main(String[] arg)
{
String str = "This is a String";
String finalStr="";
String[] arr = str.split(" ");
for(int i=(arr.length-1); i>=0; i--)
{
if(i==0)
finalStr += arr[i];
else
finalStr += arr[i] + " ";
}
//is name myhello
System.out.println("Reverse words =>"+finalStr);
}
}

- Navudu December 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In my opinion~ you should use string buffer instead of string to store the reversed string, because use "+=" to string need more space and time~

- ranran December 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

In my opinion~ you should use string buffer instead of string to store the reversed string, because use "+=" to string need more space and time~

- ranran December 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I think in the loop
if(i==0) should be if(i==arr.length-1)

- Summer January 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

public class ReverseString{
public static void main(String[] arg)
{
String str = "This is a String";
StringBuilder Finalstr=new stringbuilder();
String[] arr = str.split(" ");
for(int i=(arr.length-1); i>=0; i--)
{
if(i==arr.length-1)
finalStr.Append(arr[i])
else
finalStr.Append(" "+arr[i])
}
//is name myhello
System.out.println("Reverse words =>"+finalStr);
}
}

- Anonymous January 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.io.*;
class careercup2
{
public static void main(String args[])throws IOException
{
String s1="";
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(isr);
System.out.println("Enter a string");
String s=in.readLine();
s=s+"";
s=""+s;
for(int i=s.length();i>=0;i--)
{
while(s.charAt(i)!=' ')
{
s1=s1+s.charAt(i);
i--;
}
continue;
}
System.out.println(s1);
}
}

- Akshay December 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Well i dont have time to wite full code ,its just rough try this.
string.split(' ',"$" )
break into char array
reverse char array.tostring
replace "$" by ' '.
' '=Space..
Hope this works .

- suraj singh December 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A in-place way to do this:
1. Reverse the whole string. 'gnirtS a si sihT'
2. Reverse every 'word'. "String a is This"

- needay December 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

include <stdio.h>
#include <string.h>


inline void swap(char *a, char *b){

    char *tmp = *a;
    *a = *b;
    *b = tmp;
}


void reverse(char *start, char *end){

    while( start < end ){
        swap(start++, end--);
    }
}

void reverseWords(char *str){

    char *word = str;
    char *tmp = str;

    while(*tmp){
        tmp++;
        if(*tmp == '\0'){
            reverse(word,tmp-1);
        }
        else if( *tmp == ' '){
            reverse(word,tmp - 1);
            word = tmp + 1;
        }
    }
    reverse(str,tmp - 1);
}

- Instigo December 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This solution performs reversal in memory. First reverse the whole string. Then reverse the words in the reversed string.

"This is a test" => "tset a si sihT" => "test a is This".

#region Reversing the words in a string
        static void Main(string[] args)
        {
            //This does an in memory replacement.
            //First rever the whole string.
            //Then reverse individual words in the string.
            const string string1 = "This is a string and I am testing this";
            char[] array = string1.ToArray();
            int startIndex = 0;
            int length = array.Length;
            Console.WriteLine(array);
            Reverse(array, startIndex, length);
            int prevIndex = 0;
            while (startIndex <= array.Length)
            {
                if (startIndex == array.Length)
                {
                    Reverse(array, prevIndex, startIndex);
                    break;
                }
                if (array[startIndex] == ' ')
                {
                    Reverse(array, prevIndex, startIndex);
                    startIndex++;
                    prevIndex = startIndex;
                    continue;
                }
                startIndex++;
            }
            Console.Write(array);
            Console.ReadLine();
        }

        private static void Reverse(char[] array, int startIndex, int length)
        {
            bool isEven = (length - startIndex) % 2 == 0;
            int mid = startIndex + (length - startIndex) / 2;
            int index1 = isEven ? mid - 1 : mid;
            int index2 = mid;
            while (index1 >= startIndex && index2 < array.Length)
            {
                char temp = array[index1];
                array[index1] = array[index2];
                array[index2] = temp;
                index2++;
                index1--;
            }
        } 
        #endregion

- srihari.sridharan.r December 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

# ruby : reverse words in a string
def reverse_word(str)
  word = ""
  words = str.split(" ") 
  words.size.times { word << words.pop + " "}
  return word.chop
end

- sean December 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class MainClass1
{
	public static void main(String[] args) 
	{
		String data = "This is a program to reverse the order of words in a string";
		String tempdata = "";
		String rdata = "";
		int old_a=-1;
		int new_a=0;
		
		for(int i = 0 ; i<3;i++)
		{
			while(new_a!=(-1))
				{
				new_a = data.indexOf(' ', (old_a+1));
				
					if(new_a==(-1)) break;
				
				tempdata = data.substring((old_a+1), new_a);
				
				rdata = tempdata+ " " + rdata;
				old_a = new_a;
				
			}
		}
		rdata = data.substring((old_a+1), data.length())+" "+rdata;
		System.out.println(rdata);
		
	}
}

- Prasad Dukhande December 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String reverseWordsRecursively(String original){
		String [] splitStr = original.split(" ");
		if(splitStr.length != 1){
			int lenOfLastWord = splitStr[splitStr.length-1].length();
			return splitStr[splitStr.length-1] + " ".concat(reverseWordsRecursively(original.substring(0, (original.length() - lenOfLastWord - 1))));
		}else
			return original;
	}

- Rahul December 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// to reverse a string word by word
//for eg:arpit is---will become----is arpit
#include<stdio.h>
int main()
{
char c[30],a[30],b[30];
printf("Enter string:");
gets(a);
int s=strlen(a);
int i,j,w;
i=0;
w=0;
//(b[i]!=" ")
for(j=s;j>0;j--)
{
b[i]=a[j-1];
i++;
if(i==s)
break;
}
printf("Reversed String is:%s",b);
printf("String Reversed by each word:");
for(j=0;j<s;j++)
{
if(b[j+1]==' ' || b[j+1]==NULL)
{
for(i=j;i>=0 && (b[i]!=' ');i--)
{
//c[w]=b[i];
//w++;
//if(w>s)
//break;
printf("%c",b[i]);
}
printf(" ");
}
}
//printf("%s",c);
return 0;
}

- Arpit Jain December 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <string>
//"This is a string"
//"string a is This"
using namespace std;
int main()
{
int j=0;
string s;
string copy[80];
int pos_start=0;
int pos_end=0;
cout<<"Enter the String :: ";
getline(cin,s);
for(int i=0;s[i]!='/0';i++)
{

pos_end=s.find(" ",pos_start);
if(pos_end==-1)
{
copy[j]=s.substr(pos_start,(s.length()-pos_start));
break;
}
else
{
copy[j]=s.substr(pos_start,(pos_end-pos_start));
pos_start=pos_end+1;
j++;
i=pos_end;
}
}

for(int s1=j;s1>=0;s1--)
cout<<" "<<copy[s1];

}

- Aditya Narain January 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Java Way:

public String reverseWords(String str) {
		String[] words = str.split("\\s+");
		StringBuilder sb = new StringBuilder();
		for (int i = words.length - 1; i >= 0; i--) {
			sb.append(words[i]);
			sb.append(" ");
		}
		sb.deleteCharAt(sb.length()-1);
		return sb.toString();
	}

- neojava January 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is just one line in ruby

string.split(' ').reverse.join(' ')

- SydneyITGuy February 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Pretty simple in python too...

>>> "This is a string".split()[-1::-1]
['string', 'a', 'is', 'This']

- theodore.therone February 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In java :

String FirstString = "This is a String" ;
        String[] Nstr = FirstString.split(" "); 
  	for (int i=Nstr.length-1; i >= 0; i--){
		 System.out.print(" "+Nstr[i]); 
	  }

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

void reverse_words(string str)
{
	int len = str.length();
	for(int i = 0; i <= len / 2; ++i)
	{
		swap(str, i, len - i - 1);
	}

	int start = 0;
	for(int i = 0; i < len; ++i)
	{
		if(i == len - 1 || (str[i] != ' ' && str[i + 1] == ' '))
		{
			for(int j = start; j <= (i - start) / 2; ++j)
			{
				swap(str, j, i - j);
			}
		}

		if(i > 0 && (str[i] != ' ' && str[i - 1] == ' '))
		{
			start = i;
		}
	}
}

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

if(isset($_POST['submit']))
{
$input = $_POST['name'];
$words = explode(' ',$input);
$words = array_reverse($words);

$input = join(' ',$words);

print $input;
}

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

import java.util.HashMap;
import java.util.*;

// To execute Java, please define "static void main" on a class
// named Solution.

class Solution 
{
  public static void main(String[] args) 
  {
    String str="This is Seattle";
    String words[];
    Stack<String> wordStack=new Stack<String>();
    words=str.split(" ");
    for(int idx=0;idx<words.length;idx++)
      {
          wordStack.push(words[idx]);
      }
    
    while(!wordStack.empty())
      {
        String buf=wordStack.pop();
        System.out.println(buf);
      }
  }
}

- Ankit Malhotra March 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In js it is trivial

"This is a String".split(' ').reverse().join(' ');

- nikita.groshin July 24, 2015 | 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