Google Interview Question for Developer Program Engineers






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

The output length is n(n+1)/2 = n + (n-1) + (n-2) + ... + 2 + 1.

int k = 0;
for(int i = 0 ; i < n(n+1) / 2; i++)
 for(int j = 0; j < n-k; j++)
  output[i++] = input[j];

- S July 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
I forgot to decrease k after the second for. {{{ int k = 0; for(int i = 0 ; i < n(n+1) / 2; i++) { for(int j = 0; j < n-k; j++) output[i++] = input[j]; k--; } - S July 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Didnt follow the question :(

- Anonymous July 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry for being unclear

well i/p : cat

o/p : cat+ ca + c (skip last char and append it)
so cat skip last char =>ca append catca
now again skip last ca => c appane catcac

Hope this is clear now

- MaYanK July 29, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

How about:
1. reverse the string
2. generate suffixes of the reversed string
3. now just append each suffix while reversing the suffix
e.g.
cat -> reverse = tac
generate suffixes of tac:
1. tac
2. ac
3. c
now while appending to new string, walk through each suffix and reverse and append
1. cat 2. ca 3. c = catcac

- anna July 30, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@anna... yeah... that's a nice ideea... but I think it is too much for this problem...

- S July 30, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

So you just repeat the word using the first character as the last character?

- Anonymous July 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nope...
suppose you have the string: abcdef.
you should print:
abcdef_abcde_abcd_abc_ab_a (without _).

- S July 30, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Recursion could be used on substring of the string until length == 0

- addict July 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream> #include<string>

void print(std::string input,int len)
{
if(len == 0) {
return;
}
else {
std::cout << input.substr(0,len);
return print(input,len-1);
}
}
int main()
{
std::string a = "max";

print(a,a.length());
std::cout << "\n";
return 0;
}

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

void StrEcho(const char * str, char * output)
{
   // assume that there is enough memory at output: n(n+1)/2+1, n=strlen(str)
   for (size_t n = strlen(str); n; output += n--)
      strncpy(output, str, n);
   *output = '\0';
}

- fiddler.g July 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Now implement strncpy :)

- S July 30, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

void strncpy(char * dest, const char * src, size_t n)
{
   while (n-- && (*dest++ = *src++));
}

- fiddler.g July 30, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This shoudl work:
char *a="cat";
int size=3;
int itersize=3;
int k=0;
for (int i=0; i<size*(size+1)/2; i++)
{
cout<<a[k];
k++;
if (k>=itersize)
{
itersize--;
k=0;
}
}

- guest August 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Get the length of the input string.
2. Put the string into a queue.
3. If Queue is not empty,
.....Extract, print, and insert the elements of queue upto length - 1 times.
...Else
.....EXIT
4. Extract next element. //it will be the last element
5. Decrement length, goto 3

- D August 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

int main()
{
        int i,len=0;
        char input[80];
        memset(input,0,(80));
        scanf("%s",input);

        len = strlen(input);

        while(len)
        {
                for(i=0;i<len;i++)
                        printf("%c",input[i]);
                len--;
        }
        printf("\n");
        return 0;
}

- D August 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

string echo(string str)
{
  if(str.length() == 1)
     return str;
  retrun str + echo(str.substr(0,str.length()-1));
}

- ahj August 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Simple dudes...
Push the characters of the string into the stack.
Ex: C(1st) A(2nd) T(3rd)
Now print the entire stack from bottom, perform a pop and print again. continue this till the stack is empty.
tats it lol!....

- Sunil August 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

how do you print stack from the bottom???

- Anonymous September 19, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The above logic is using DS.
ELSE u can go with logic of "ahj". Its pretty simple in Java

- Sunil August 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What about, something like this?

public static void printChain(String w){
        for(int i=w.length();i>0;i--){
            String sub=w.substring(0,i);
            System.out.print(sub);
        }
    }

- markos September 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String print(String s, int end){
    if(end==1)
	return s.substring(0, end);
    else
        return s.substring(0, end) + print(s,end-1);
}	

String echo(String s){
     return print(s, s.length());
}

- arv October 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Eyes-closed in Python: return inputstring + inputstring[0:2] + inputstring[0]

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

#In Perl
use strict;
my $word = shift;
my $out = '';
while($word)
{
    $out = $out.$word;
    chop $word;
}
print $out;

- Odi December 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a c# recursive implemntation

public static string func(string str)
        {
            if (str == "")
            {
                return str;
            }
            return str + func(str.Substring(0, str.Length - 1));
        }

- shahar November 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String input = "cat";
		int k = 0, N = input.length();
		
		for(int i = N; i > 0; i--)
			for(int j = 0; j < i; j++)
				System.out.print(input.substring(j, j+1));

- vkapko February 06, 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