HCL Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

You can use xor swapping.

public void reverse(char s[]){
	for(int i=0,j=s.length-1;i<=j;i++,j--){
		s[i] ^= s[j]; 
		s[i] ^= (s[j] ^= s[i]);
	}
}

- srdjan August 31, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

The above is the correct solution. You can go one step further to demonstrate total mastery by mentioning that the XOR swap is sub-optimal on modern processor architectures due to them utilizing parallel processing to enhance these types of swap operations. Older architectures actually saw substantial gains from utilizing XOR due to their linear instruction processing limitation, small cache sizes, and slower memory access times.

For example:

Old Architecture:
a = 1;
b = 2;
XOR completes in 3 sequential processes.

Newer architecture:
a=1;
b=2;
temp;
Parallel processing completes in 2 processing steps.
1. Move a to temp
2. Move a=b AND b=temp in parallel (hyperthreading/multi-core)

- masterjaso August 31, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

it says no local var, and you are using two in the loop, i and j. If that's valid, then you could also declare "k" and use it inside for swapping.
Probably the expected answer, but the formulation is wrong.

- X September 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

There is a bug in this solution. The condition to stay in the loop should be i<=j, i!=j does not work when there are even number of characters.

- Daniel 4 of 4 vote You can use xor swapping. September 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Daniel: Tnx, updated it :)

- srdjan September 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

For some reason this code does not work when strlen results in an odd number.

- Vinesh Balan October 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Vinesh, that's a test of debugging skills :) ending condition should be i<j instead of i<=j, so the middle element doesn't xor with itself?

- coffeemug November 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This seems to work even when the values are equal. Well done!

- Anonymous March 30, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

we've to do the code without using temp variable...

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

public static String reverseStringRecursion(String str){
if(str.length() == 1){
return str;
}
return (reverseStringRecursion(str.substring(1)) + str.charAt(0));
}

- ouyangwanbin September 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if the types are important, then you need to use an array of chars. this should make the str.substring part difficult.

- paul.nikonowicz September 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The crux of the problem is we need to swap two numbers with out using a temp variable. By traversing the string array in two directions and swap the variable. Here the swapping will takes place with out any temp variable. The swapping algorithm can looks like below

Swap( int *a, int *b)
{
*a = *a + *b;
*b = *a - *b;
*a = *a - *b;
}

Sunil

- Anonymous September 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

a* = a* - b*;
b* = a* + b*;
a* = b* - a*;

- Alternatively, September 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

# include <stdio.h>

/* Function to print reverse of the passed string */
void reverse(char *str)
{
if(*str)
{
reverse(str+1);
printf("%c", *str);
}
}

/* Driver program to test above function */
int main()
{
char a[] = "hello";
reverse(a);
getchar();
return 0;
}

- Bharathwaj N R September 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you know what? you are not reversing the string but printing the string in reverse order, this is not desired. but we have to reverse the string element so that there will be a reverse changing in the original string..

- vikassanmacs September 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi..im trying to use while(*str) instead of if(*str) to print a given string in reverse order. But its running in infinite loop. what is the issue in using a while loop?

- Divya August 07, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

geekyjumps.blogspot.in/2013/09/reverse-given-string-without-using.html

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

reverse( SqList & L )
{
	int i = 0; 
	int j = L.length - 1;
	while( i < j )
	{
		L.length[i] = L.length[i]^L.length[j];
		L.length[j] = L.length[i]^L.length[j];
		L.length[i] = L.length[i]^L.length[j];
		i++;
		j--;
	}
}

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

This is tested with all possibilities. xor swapping is used which will be done n/2 times. (O(n))

public void reverseString(char str[]) {
		for (int i = 0; i < str.length / 2; i++) {
			str[i] ^= str[str.length - 1 - i];
			str[str.length - 1 - i] ^= str[i];
			str[i] ^= str[str.length - 1 - i];
		}
	}

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

Guys this is "C" Language column, please dont forget this :)

- Rick Ro$$ September 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if it is a C string, we could use the null char as a temporary location.

- kvh September 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is what I did..

- Justin November 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can first find the length of the string and then from the next character of the string(null character) we append the characters in reverse.... now finally we copy these reversed characters back into index 0 to The length of the string .

- Nishant Mintri September 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Scanner;

public class StringRev {


public static void main(String[] args) {

String rev="";

Scanner scanner=new Scanner(System.in);

System.out.println("enter a string");

String org=scanner.next();

int length=org.length();

for(int i=length-1;i>=0;i--){


rev=rev+org.charAt(i);

System.out.println(rev);
}

- Roshan Dsouza October 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<string.h>
int main()
{
	char a[] = "welcome";
	int i,j;
	for(i=0,j=strlen(a)-1;i<j;i++,j--)
	{
		a[i] ^= a[j];
		a[j] ^= a[i];
		a[i] ^= a[j];
	}
	printf("%s",a);
	return 0;
}

- sakthivel Sethu,Software Engineer ,Bangalore October 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is just pseudo code i have not checked the code but idea is to using shift operator .

In given string search for the null pointer'\0'

And apply Left shift operator <<=.


main()
{
string str[30];
int i;

printf("Enter the tring:\n");
for(i=0;i<n;i++)
{
scanf("%s",str[i]);
}


if(str[i]=='\0')
{for(j=0;j<n;j++)
{
str[i]>>=str[n-j];
printf("Reversed string %s", str);
}}

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

This is just pseudo code i have not checked the code but idea is to using shift operator .

In given string search for the null pointer'\0'

And apply Left shift operator <<=.


main()
{
string str[30];
int i;

printf("Enter the tring:\n");
for(i=0;i<n;i++)
{
scanf("%s",str[i]);
}


if(str[i]=='\0')
{for(j=0;j<n;j++)
{
str[i]>>=str[n-j];
printf("Reversed string %s", str);
}}

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

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

int main()
{
char str[] = "hello";
int s,i;

s=strlen(str);

for(i=s;i>=0;i--)
printf("%c",str[i]);

getch();
return 0;

}

- joker November 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char a[]=="Hello";
gets(a);
for(i=strlen(a)-1;i>=0;i++)
{
printf("%s",a[i]);
}

- @Netesh November 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>

#define SIZE 15

void exchange(char str[]);

void main()
{
char str[SIZE+1]; // for NULL

printf("Enter a string: ");
scanf("%s", str);

exchange(str);

printf("Reversed string: %s\n", str);

return 0;
}

void exchange(char str[])
{
int i, n;

for (i = 0, n = (strlen(str) - 1); i < strlen(str) / 2; i++)
{
str[i] ^= str[n-i];
str[n-i] ^= str[i];
str[i] ^= str[n-i];
}
}

- Swaroop November 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    int i,len;
    char str[]="Hello";
    len=strlen(str);
    printf("Length of String is = %d",len);
    
    for(i=len;i>=0;i--)
    printf("%c",str[i]);
        
    return 0;
}

- Ankit Lalan November 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    int i,len;
    char str[]="Hello";
    len=strlen(str);
    printf("Length of String is = %d",len);
    
    for(i=len;i>=0;i--)
    printf("%c",str[i]);
        
    return 0;
}

- Ankit Lalan November 18, 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 *s, int start, int end){

if(start < end){
s[start] = s[start] + s[end];
s[end] = s[start] - s[end];
s[start] = s[start] - s[end];
reverse(s, start+1, end-1);
}
}

int main()
{
char str[] = "Hello";

reverse(str, 0, strlen(str)-1);
printf("%s", str);

return 0;
}

- Ashish January 19, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think,all of you are not focusing on questions....The question is without using temporary variable...but no answer posted regarding without using temp variable.

- Prashant July 31, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<string.h>
void rev(char a[],int n);

int main()
{
char a[]="this is my string";
int n=sizeof(a);
rev(a,n);
printf("\n\n");
return 0;
}

void rev(char a[],int n)
{
int i,m=n;char t;
for(i=0;i<n/2;i++)
{
t=a[i];
a[i]=a[n];
a[n]=t;
n--;
}

for(i=0;i<m;i++)
printf("%c",a[i]);

}

- Anonymous August 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<string.h>
void rev(char a[],int n);

int main()
{
char a[]="this is my string";
int n=sizeof(a);
rev(a,n);
printf("\n\n");
return 0;
}

void rev(char a[],int n)
{
int i,m=n;char t;
for(i=0,n=strlen(a)-1;i<n/2;i++)
{
t=a[i];
a[i]=a[n-i];
a[n-i]=t;

}

for(i=0;i<m;i++)
printf("%c",a[i]);

}

- Anonymous August 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
2 #include <string.h>
3 #include <errno.h>
4
5 void
6 strrev_helper( char *str )
7 {
8 if ( strlen( str ) <= 1 ) {
9 return;
10 }
11
12 str[strlen(str)-1] ^= str[0];
13 str[0] ^= str[strlen(str)-1];
14 str[strlen(str)-1] ^= str[0];
15
16 strrev_helper( str + 1 );
17
18 return;
19 }
20
21 void
22 strrev( char *str )
23 {
24 if ( strlen( str ) <= 1 ) {
25 return;
26 }
27
28 strrev_helper( str );
29
30 strrev( str + 1 );
31
32 return;
33 }
34
35 int
36 main( int argc, char *argv[] )
37 {
38 char *str;
39
40 if ( argc < 2 ) {
41 fprintf( stderr, "%s <string>\n", argv[0] );
42 return ( -EINVAL );
43 }
44
45 str = argv[1];
46
47 strrev( str );
48
49 printf( "%s\n", str );
50
51 return ( 0 );
52 }

- yki September 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void reverse(char *a)
{
int b,d;
b=strlen(a);
d=b;
b=b-1;
p=r=a;
r=r+b;
d=d/2;
while(d>0)
{
*p=*p+*r;
*r=*p-*r;
*p=*p-*r;
p=p+1;
r=r-1;
d--;
}

- mithilesh kumar (tict kolkata) November 07, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void reverse(char *a)
{
int b,d;
b=strlen(a);
d=b;
b=b-1;
p=r=a;
r=r+b;
d=d/2;
while(d>0)
{
*p=*p+*r;
*r=*p-*r;
*p=*p-*r;
p=p+1;
r=r-1;
d--;
}

- mithilesh kumar (tict kolkata) November 07, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void reverse(char *a)
{
int b,d;
b=strlen(a);
d=b;
b=b-1;
p=r=a;
r=r+b;
d=d/2;
while(d>0)
{
*p=*p+*r;
*r=*p-*r;
*p=*p-*r;
p=p+1;
r=r-1;
d--;
}

- mithilesh kumar (tict kolkata) November 07, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<string.h>
#define max 100
int main()
{
char a[max];
int i,j,flag=0;
gets(a);
for(i=0,j=strlen(a)-1;i<(j/2);i++,j--)
{
if(a[i]!=a[j])
{
flag=0;
break;
}
else
flag=1;
}
if(flag==1)
{
printf("string is palindrom\n");
}
else
printf("string is not palindrom\n");
printf("%s\n",a);
printf("reverse string is\n");
for(i=0,j=strlen(a)-1;i<j;i++,j--)
{
a[i] ^= a[j];
a[j] ^= a[i];
a[i] ^= a[j];
}
printf("%s",a);
return 0;
}

- Mayank Madhukar August 01, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char str[]="Hello";
for(i=strlen(str);i>=0;i--)
printf("%c",str[i]);

return 0;
}

- Anonymous September 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char str[]="Hello";
for(i=strlen(str);i>=0;i--)
printf("%c",str[i]);

return 0;
}

- Raghava September 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a = 'c'
b = 'x'

print("before swaping values are",a,b)

a += b
b = a[:len(a)-len(b)]
a = a[len(b):]

print("after swaping values are",a,b)

- kunal September 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a = 'c'
b = 'x'

print("before swaping values are",a,b)

a += b
b = a[:len(a)-len(b)]
a = a[len(b):]

print("after swaping values are",a,b)

- kunal jamdade September 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
char str[]="hello";
int i,k;
clrscr();
k=strlen(str);
cout<<k;
for(i=k-1;i>=0;i--)
{
cout<<str[i];
}
getch();
}
//cooool

- Bharathwaj N R August 31, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi Bharthwaj, here the problem is to reverse the string but not to print in reverse order.

- CodeCrazy August 31, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Hope this works correctly .

public static void main(String[] args) {
StringBuffer str= new StringBuffer("Amit");
StringBuffer str1= str.reverse();
System.out.println(str1);

}

- Amit Kumar August 31, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

no interviewer wants you to use standard library functions on these types of questions

- Miguel Oliveira September 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

char temp;
j=strlen(str);
for(i=0;i<j-1;i++)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
j--;
}

- nikhil.kathare September 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

kj.. njk////////////////////////////b

- Anonymous November 18, 2013 | Flag


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