Flipkart Interview Question for Software Engineer / Developers






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

{

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

void rev_words_in_sentence(char *curr){
char *last = curr;
while(*curr != '\0'){
if(*curr == ' ' && *last != ' '){
str_rev(last, curr-1);
last= curr + 1;
}
else if(*curr == ' ' && *last == ' '){
last++;
}
curr++;
};
str_rev(last, curr-1);
};

int main(){
char ptr[] = "We apologise for the inconvenience";
printf("%s\n", ptr);
rev_words_in_sentence(ptr);
printf("%s\n", ptr);
return 1;
};
}

- srinivas December 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String reverseString(String word) {
int len = word.length();
StringBuffer buff = new StringBuffer(len);
for (int i = (len - 1); i >= 0; i--)
buff.append(word.charAt(i));
return buff.toString();
}

- Anonymous December 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

//reverse a sent

void reverseWord(char* word, int n){
int i=0,j=0;
char temp = ' ';
for (i=0, j=n-1; i <=j ; i++,j--){
temp = word[i];
word[i] = word[j];
word[j] = temp;
}
printf("%s\n",word);
}

void reverseSent(char * str){
int i = 0, count = 0;
while (str[i] != '\0'){
if (str[i] == ' ' || str[i] == ','|| str[i] == ';' || str[i] == ';'){
reverseWord(str +i -count, count);
count = 0;
}
else {
count++;
}
i++;
}
reverseWord(str +i -count, count);
}

int main()
{
char sent[] = "I am a sexy girl";
reverseSent(sent);
printf("%s\n",sent);
return 1;
}

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

Inplace processing.. :


public static void main(String a[]) {
reverseWords("this is a test ".toCharArray());
}

private static void reverseWords(char[] arrayOfChars) {
for (int i = 0; i < arrayOfChars.length; i++) {

for (int j = i; j < arrayOfChars.length; j++) {
if (arrayOfChars[j] == ' ' || arrayOfChars.length == j) {
int lastLetterInWord = j;
j--;
while (i < j) {
swap(arrayOfChars, i, j);
i++;
j--;
}
i = lastLetterInWord;
break;
}
}
}
System.out.println(" TEST : " + new String(arrayOfChars));
}

- Guru April 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In Java, StringBufer has a reverse function - can we just use that?

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

<pre lang="c#" line="1" title="CodeMonkey74369" class="run-this">using System.Text;
class Main
{
public void WordWiseRev(string s)
{
s = s + " ";
char[] original = s.ToCharArray();
StringBuilder temp = new StringBuilder();
StringBuilder result = new StringBuilder();
int i = 0;
int n = original.Length;
int m = n - 1;
char c = '\0';
for (i = 0; i < n; i++)
{
c = original[i];
if (c == ' ')
{
result.Append(temp);

if (i < m)
{
result.Append(c);
}

temp.Clear();
}
else
{
temp.Insert(0, c);
}
}

Console.WriteLine(result);
}

public static void Main ()
{
string s = "i am a fool";
WordWiseRev(s);
Console.ReadLine();
}
}

</pre><pre title="CodeMonkey74369" input="yes">
</pre>

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

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


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

	int i=0;
	char temp;
	if(end<=start) return;
	
	while(i<=(end-start)/2)
	{
		temp = arr[start+i];
		arr[start+i]=arr[end-i];
		arr[end-i]=temp;
		i++;
	}
}


void reverse_string(char *arr)
{
	int first=0, last=0;
	
	while(1)
	{
		while(arr[last]!='\0' && arr[last]!=' ')
			last++;
		reverse(arr, first, last-1);
		if(arr[last]=='\0') return;
		
		first=last+1; last++;
	}


}

int main()
{
	char *foo = "I am a fool";
	
	printf("before: foo[%s]\n", foo);
	
	reverse_string(foo);
	
	printf("aftre: foo[%s]\n", foo);

	return 0;
	
}

Complexity: O(n)

- shoonya.mohit July 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

import java.util.StringTokenizer;

public class Test {
public static void main(String[] args){
String s = "I am a fool";
StringBuilder r = new StringBuilder();
StringTokenizer tok = new StringTokenizer(s," ");
while (tok.hasMoreElements()) {
String s1 = tok.nextToken();
for(int i = s1.length()-1;i>=0;i--){
r.append(s1.charAt(i));
}
r.append(" ");
}
System.out.println(r);
}
}

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

Solution in C:


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

void StrRev(char *Str, int st, int en)
{
char tmp;
while(st < en)
{
tmp = Str[st];
Str[st] = Str[en];
Str[en] = tmp;

st++;
en--;
}
}

void WordRevInSent(char *Str)
{
int i=0,j=0;
while(1)
{
if(Str[i] == ' ')
{
StrRev(Str, j, i-1);
j = i+1;
}
else if (Str[i] == '\0')
{
StrRev(Str, j, i-1);
Str[i] = '\0';
break;
}
i++;
}
}
int main()
{
char Sent[100];

printf("Enter String: ");
gets(Sent);
WordRevInSent(Sent);
puts(Sent);
getch();
return 0;
}

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

One liner in perl:
$revwords = join(" ", reverse split(" ", $string));

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

Python implementation

def reverseString(string):
result_string=""
for i in string.split(' '):
result_string+=i[::-1]+' '

return result_string.strip()

print reverseString("I am a fool")

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

$input = $_POST['name'];
		$output = strrev($input);
		
		$words = explode(' ',$output);
		$words = array_reverse($words);
		
		$output = join(' ',$words);
		print $output;

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

s = raw_input().split(' ')
S = []
for i in range(len(s)):
S.append(s[i][::-1])
print ' '.join(S)

- Anonymous December 04, 2016 | 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