GE (General Electric) Interview Question for Software Engineer / Developers






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

Hey this can be done in various ways...the best approach is to maintain distinct char pointers to individual words in the sentence and then form new words for specified order by assigning the words pointed by those particular pointers in the new sentence.
For example, for sentence char *input = "i am an indian"; there can be 4 char pointers declared, say
char *p1,*p2,*p3,*p4;
then assign pointers such that they should point to individual words, i.e.
p1 = "i" , p2 = "am" , p3 = "an" , p4 = "indian"

and then declare new sentence as follow
char* new = (char *) malloc( (strlen(input)+1)*sizeof(char) );
Note: size is inputstring lenght + 1 to accomodate the null character at the end.

Now, start assigning individual words to this new *empty* string
pointed by char * output.

This is the algorithm..

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

cfafa

- Anonymous May 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the code that I have tested with GCC..it work superb, complexity is O(2n)..


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


char * reverse(char *input)
{
char *tmp = input;
int count = 0;
while(tmp && *tmp)
{
if (*tmp == ' ') count++;
tmp++;
}
char *p[++count];
char *new = (char *) malloc((strlen(input)+1)*sizeof(char));
char *output = new;
p[0] = input;
int i=1;
while(input && *input && i<count)
{
if (*input == ' ')
{
p[i++] = ++input;
}
else
{
input++;
}
}
int j;
for(j=count-1;j>=0;j--)
{
while(*p[j] != '\0' && *p[j] != ' ' )
{
*new++ = *p[j]++;
}
*new++ = (j > 0)? ' ':'\0';
}
return output;
}


int main()
{
char y[] = "I am an Indian";
printf("%s\n",y);
char *x = reverse(&y[0]);
while(x && *x)
{
printf("%c",*x);
x++;
}
return 0;
}

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

Hi codegeek,
THe solution u have provided is fine, but u have to do it without extra space. The sort should be inplace sort

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

1. Reverse each word - O(n) time, O(1) space
2. Reverse all sentence - O(n) time, O(1) space

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

The solution u have provided does not satisfy the condition i have given above

- Bhargava December 31, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Algo:
for the first output push first string in the stack & print rest & then pop the stack.
for the 2nd output push 2 string & print the remaining & likewise for the 3rd output.
java code:
public static void main(String[] args) {
String a = "I am an indian";
Stack<String> stack = new Stack<String>();
String[] temp;
String delimiter = " ";
temp = a.split(delimiter);
for(int i=0;i<temp.length-1;i++){
int k=i;

for(int l = 0;l<=k;l++){
stack.push(temp[l]);

}

for(int j=i+1;j<temp.length;j++){
System.out.print(temp[j]+" ");
}
while(!stack.isEmpty()){
System.out.print(stack.pop()+" ");
}
System.out.println();
}

}




}

- snehlata June 26, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

assume S is the string array of words....size of S is n.

for i = n-1..1
S = reverse(reverse(S[0]) reverse(S[1..i]))
print S

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

ROtate array {0,1,2,3}. 0 index of first word, 1 index of second word etc..
Function(int[] array, int rotateindex)

I will call this function for all the rotateindex i.e. 0 to 4 and based on the final rotated array. i will outpout the word at the indexex to the console.

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

Hi,
For in place reversal,
1. Reverse complete sentence
2. Reverse each word individually

time complexity O(n)
space O(1)

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

Depends on how the string is presented, as an array of characters or as a linkedlist of words..
My best bet would be to convert the given thing into a linked list of words, and then manipulate the string accordingly using 2 pointers, one at the end, i.e. at the word 'indian' and one at the starting of the sentence..

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

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

void reverse(char *str)
{
int ii;
char temp;
int len = strlen(str) - 1;
for(ii = 0; ii < len; ii++, len--)
{
temp = str[ii];
str[ii] = str[len];
str[len] = temp;
}
}

int main(int argc, char *argv[])
{
char str[] = "Hi a this is Lakshman";
int len = strlen(str);
reverse(str);

char *token = NULL;
for(token = strtok(str, " "); token != NULL; token = strtok(NULL, " "))
reverse(token);
int ii;
for(ii = 0; ii != len; ii++)
{
if(str[ii] == '\0')
str[ii] = ' ';
}

printf("%s\n", str);
return 0;
}

- mlakshmanarao August 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package demo;

import java.util.Scanner;

public class JavaDemoTwo {
	private static String original = "";
	public JavaDemoTwo() {
		// TODO Auto-generated constructor stub
	}
	
	static void readInput(){
	    Scanner in = new Scanner(System.in);	 
	    System.out.print("Enter a string to reverse : ");
	    original = in.nextLine();
	    in.close();
	}
	
	static void reverseString(){ 
		String reverse = "";
	    int length = original.length();	 
	    for ( int i = length - 1 ; i >= 0 ; i-- )
	         reverse = reverse + original.charAt(i);	 
	    System.out.println("Reverse of entered string is: "+reverse);
	    
	}
	
	static String rearrangeStrings(String myString){
		int indexOfSpace = myString.indexOf(" ");
		String finalString = myString.substring(indexOfSpace  + 1)+" "+myString.substring(0, indexOfSpace);
		original = finalString;
		return original;
	  
	}

	
	public static void main(String[] args) {
		readInput();
		System.out.println("input string : " + original);
		reverseString();
		for(int i=0;i<original.split(" ").length-1;i++ )
			System.out.println(rearrangeStrings(original));

	}

}

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

Algo:
split the input string on space delimiter.
for fisrt output push fisrt string in stack & then print remaining strings & then pop the stack.
for 2nd output push 2 strings in stack & print remaining & then pop both the string from stack & like wise for the 3rd output ,push 3string in stack & print remaining & then pop the stack with all three strings.

Here goes the java code:
public class Stoken{
public static void main(String[] args) {
String a = "I am an indian";
Stack<String> stack = new Stack<String>();
String[] temp;
String delimiter = " ";
// given string will be split by the argument delimiter provided.
temp = a.split(delimiter);
for(int i=0;i<temp.length-1;i++){
int k=i;

for(int l = 0;l<=k;l++){
stack.push(temp[l]);

}

for(int j=i+1;j<temp.length;j++){
System.out.print(temp[j]+" ");
}
while(!stack.isEmpty()){
System.out.print(stack.pop()+" ");
}
System.out.println();
}


}




}

- snehlata June 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is a clean C++ solution. Tested with VS.

#include <iostream>

using namespace std;

int cnt;

void SortWord(char str[], int len)
{
	char *temp = new char[len];
	memset(temp, 0, len);

	for (int i = 0; str[i] != '\0' && cnt > i; i++)
	{
		if (str[i] == ' ') {
			memcpy(temp, &str[i + 1], len - i - 1);

			str[i] = '\0';
			temp[len - i - 1] = ' ';
			
			memcpy(&temp[len - i], str, i);
			temp[len] = '\0';

			memcpy(str, temp, len);

			cout << str << endl;

			cnt = cnt - i - 1;
			SortWord(str, len - i - 1);

			break;
		}
	}
}

int main()
{
	char input[] = "this is a test";
	int len = strlen(input);
	cnt = len;

	SortWord(input, len);

	return 0;
}

- sam October 31, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

the input string can be changed. A sample output of string "this is a test of fun" looks like:

is a test of fun this
a test of fun is this
test of fun a is this
of fun test a is this
fun of test a is this

- sam October 31, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

fix the memory leak.
change the two lines:

char *temp = new char[len];
	memset(temp, 0, len);

to:

char temp[256];

- sam October 31, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why not use a circular queue.

- RK June 15, 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