HCL Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

Well this's a easy question to ask..

Algorithm is:
1. Reverse the whole string.
2. Reverse the individual words.

For example:
Welcome to India
after step 1 => aidnI ot emocleW
after step 2 => India to Welcome
...

0(n) and not extra memory... :)

- coding.arya May 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
3
of 3 vote

Push all the words in a stack and then pop them.

- alex May 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@alex: Hi alex, we need to do word reversal, not string reversal.

- dadakhalandhar May 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

dude, this gives word reversal. If you push entire words into stack (words as a whole), how can u get string reversal?? eg
How are you
stack: you (top)
are
How

- alex May 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@alex. got it

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

Great idea, but if you are using C pointer (hence the language is C or C++), this requires you to implement a stack (well, it's very easy, but still) of Strings. + It takes up extra memory.

Using C pointer, reserving the whole string, then reverse individual words is better.

- Mo Lam May 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void reverse(char* sta,char* end)
{
	while(sta<=end)
	{
		char c = *sta;
		*sta = *end;
		*end = c;
		sta++;
		end--;
	}
}

void reverseWords()
{
	char str[MAX_LEN];
	printf("Enter the string: ");
	fgets(str,MAX_LEN,stdin);
	char *sta,*end;
	int len = strlen(str);
	sta = str;
	end = str+strlen(str)-2;
	*(end+1) = '\0';	
	reverse(sta,end);
	sta = strtok(str," ");
	while(1)
	{
		end = sta+strlen(sta)-1;
		reverse(sta,end);
		sta = strtok(NULL," ");
		if(sta == NULL)
			break;
	}
	for(int i=0;i<len;i++)
		printf("%c",str[i]);
	printf("\n");
}

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Class4
{
static void Main(string[] args)
{

String s = "Welcome to India";

char[] c = new char[s.Length];

int count = 0;
int j = 0;
int a = 0;


for (int i = s.Length-1; i >= 0; i--)
{
count++;

if (s[i] == ' '||i==0)
{

if (i == 0)
j = i;
else
j = i+1;

while (count > 1)
{

c[a] = s[j];
j++;
a++;
count--;
}

c[a] = ' ';
count = 0;
a++;
}

}

for (int i = 0; i < c.Length; i++)
Console.Write(c[i]);

Console.ReadLine();

}
}
}

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

A modified recursion can help here...

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

bad coding but works

int j=0; //global counter
void foo(char *s, char *t)
{
        if(*s == '\0')
                return;
        foo(++s, t);
        if(*s)
                t[j++] = *s;
}

char sum[100];
int main()
{
        char t[100];
        int i=0, k=0;
        int count=0;
        char string[] = {"wel to india is goign and I am anish"};

        t[strlen(string)-1] = string[0];
        foo(string, t);
        t[++j] = '\0';
        strcpy(string, t);

        memset(string, 0, strlen(string));
        j=0;
        for(i=0;1;) {
                if(*(t+i) == '\0') {
                        string[k] = '\0';
                        sum[j+strlen(string)-1] = string[0];
                        foo(string, sum);
                        sum[++j] = '\0';
                        break;
                } else if(!isspace(*(t+i)))
                        string[k++] = *(t+i);
                else if(isspace(*(t+i))){
                        string[k] = '\0';
                        k=0;
                        sum[j+strlen(string)-1] = string[0];
                        foo(string, sum);
                        sum[++j] = ' ';
                        j++;
                }
                i++;
        }
        printf("%s\n", sum);
        return 0;
}

- aka May 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace testApplication
{
class Program
{
static void Main(string[] args)
{
string _name = "test string to test reverse order";
string[] splittedString = _name.Split(' ');
Console.WriteLine(_name);
for (int index = splittedString.Length-1; index >= 0; index--)
{
Console.Write(splittedString[index]+" ");
}
Console.ReadLine();
}
}
}

- Tarun Chugh May 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{
class RevStr{
public void reverseString(String str){
Map<Integer,String> map = new LinkedHashMap<Integer, String>();
StringTokenizer tok = new StringTokenizer(str, " ");
int i = 1;
while(tok.hasMoreTokens()){
map.put(i, tok.nextToken());
i++;
}
String temp = "";
for(i = 0; i < map.size(); i++ ){
temp = temp + map.get((map.size()-i)) + " ";
}
System.out.println(temp);
}
public static void main(String[] args) {
reverseString("Welcome to India");
}
}
}

- Rahul Singh Rathore May 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void reverseString(char []str){

int k = 0;
int l = k;
char ch;
for(int i = 0; i < str.length; i++){
if(str[i] == ' ' || str[i] == '\0'){
for(int j = i - 1; j>=(i+k)/2; j--){
ch = str[j];
str[j] = str[l];
str[l] = ch;
l++;
}
str[i] = ' ';
k = i + 1;
l = k;
}
if( i == (str.length - 1)){
for(int j = i + 1 - 1; j>=(i + 1 + k)/2; j--){
ch = str[j];
str[j] = str[l];
str[l] = ch;
l++;
}
}
}
k = 0;
char []str2 = new char[str.length];
for(int m = str.length -1 ; m >=0; m--)
str2[m] = str[k++];
String str1 = new String(str2);
System.out.println(str1);
}

- Rahul Singh Rathore May 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
void reverse(char *str,int begin,int end)
{
char temp;
int b,e;
b = begin;
e = end;
if(str==NULL)
return;
for(;b<e;b++,e--)
{
temp = str[e];
str[e] = str[b];
str[b] = temp;
}
}
void reverseEveryWord(char *str)
{
int temp=0,b=0;
char *start=str;
if(start==NULL)
return ;
while(*start)
{
if(*start!=' ')
{
temp++;
start++;
}else
{
reverse(str,b,temp-1);
temp++;
b = temp;
start++;
}
}
reverse(str,b,temp-1);
}
int main()
{
char str[100];
int length;

printf("Input a string please:\n");
while(gets(str)!=EOF)
{
length = strlen(str);
reverse(str,0,length-1);
reverseEveryWord(str);
printf("After Worlds Reverse:\n%s\n",str);
printf("Input a string please:\n");
}
return 0;
}

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

#include<stdio.h>
void main()
{
int i=0,j;
char *a="Welcome to India";
printf("enter a string ");
scanf("%s",a);
while(*(a+i)!=NULL)
i++;
for(j=i-1;j>=0;j--)
printf("%c",*(a+j));
}

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

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

void reverse(char *inp_str)
{
char * pch=strrchr(inp_str, ' ');
int position = 0;
if ( pch!=NULL && *pch != '\0' ){
position = pch-inp_str;
while (pch!=NULL && *pch != '\0'){
pch++;
printf ("%c", *pch);
}
printf (" ");
inp_str[position] = '\0';
reverse(inp_str);
} else {
printf ("%s\n", inp_str);
}
}

main()
{
char inp_str[] = "Welcome to India";
reverse(inp_str);
return 0;
}

- Sunil Mahato February 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. reverse the whole string.
2. reverse the words in this string

#include <iostream>
#include <new>
#include <cstdlib>
#include <cstdio>
#include <string>
using namespace std;

void reverse(string &s, int st, int end);

int main()
{
	string s = "Welcome to Bangladesh";
	int i, j = s.length()-1;
	i = 0;
	//cout<<s<<endl;
	reverse(s, i, j);
	cout<<s<<endl;
	int st, end;
	bool started = false;
	for(i = 0; i<=j; i++){
	    if(isalpha(s[i]) && started == false){
	        started = true;
	        st = i;
	        cout<<"s[i] = "<<s[i]<<endl;
	    }
	    if(s[i] == ' '){
	        if(started){
	            end = i;
	            reverse(s, st, end-1);
	            cout<<"temporary after reverse: "<<s<<endl;
	            started = false;
	        }
	    }
	}
	if(started){
	    reverse(s, st, i-1);
	}
	cout<<"Reversed words "<<s<<endl;
	return 0;
}

void reverse(string &s, int st, int end){
    char temp;
    int i, j;
    cout<<s<<"st="<<st <<", end= "<<end<<endl;
    while(st<end){
	    temp = s[st];
	    s[st] = s[end];
	    s[end] = temp;
	    st ++;
	    end --;
	}
	cout<<s<<endl;
}

- ijsumana March 02, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

in C:

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

void reverse(char *s, int st, int end);

int main()
{
	char s[10000];// = {"Welcome to Bangladesh"};
	scanf("%[^\n]s", s);
	int n = strlen(s);
	int i = 0;
	reverse(s, i, n-1);
	int st, end;
	int started = 0;
	for(i = 0; i<=n-1; i++){
	    if(isalpha(s[i]) && started == 0){
	        started = 1;
	        st = i;
	    }
	    if(s[i] == ' '){
	        if(started){
	            end = i;
	            reverse(s, st, end-1);
	            started = 0;
	        }
	    }
	}
	if(started){
	    reverse(s, st, i-1);
	}
	printf("Reversed words %s\n",s);
	return 0;
}

void reverse(char *s, int st, int end){
    char temp;
    printf("s = %s st=%d end = %d\n", s, st, end);
    while(st<end){
	    temp = s[st];
	    s[st] = s[end];
	    s[end] = temp;
	    st ++;
	    end --;
	}
	printf("%s\n", s);
}

- ijsumana March 02, 2017 | 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