Google Interview Question
Developer Program EngineersSorry 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
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
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';
}
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!....
The output length is n(n+1)/2 = n + (n-1) + (n-2) + ... + 2 + 1.
- S July 29, 2010