HCL Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
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)
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.
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.
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?
public static String reverseStringRecursion(String str){
if(str.length() == 1){
return str;
}
return (reverseStringRecursion(str.substring(1)) + str.charAt(0));
}
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
# 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;
}
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..
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);
}
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);
}}
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);
}}
#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];
}
}
#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;
}
#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]);
}
#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]);
}
#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 }
#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;
}
#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
You can use xor swapping.
- srdjan August 31, 2013