## Sap Labs Interview Question for Software Engineer / Developers

Country: United States
Interview Type: Phone Interview

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

bool stringCom(std::string s, std::string s1) {
if (s.length() != s1.length()) {
return false;
}
for ( int i = 0; i< s.length(); i++ ) {
if ( (s.c_str()[i] - s1.c_str()[i]) ) {
return false;
}
}
return true;
}

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

It seems, interviewer is asking to implement equals method. in this way @sjain's solution is better.

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

Updated after sjain comment.

int compareStrings(const char* str1, const char* str2) {
if(strlen(str1) != strlen(str2)) return false;
for(int i = 0; i < strlen(str1); i++) {

if(str1[i] < str2[i] || str2[i] < str1[i]) {
return 1;
}
}
return 0;
}

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

why need to have a extra space..?

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

We can put first string in a Set and after adding second string, if Set's size is 1 then strings are equal, otherwise unequal.

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

You can something like this:

``````public boolean compareString(String str1,String str2){

int bool[] = new int[256];
for(int i=0;i<256;i++) bool[i]=0;
for(int i=0;i<str1.length();i++){	bool[str1.charAt(i)]++; 	}
for(int i=0;i<str2.length();i++){	bool[str2.charAt(i)]--;     }
for(int i=0;i<256;i++){	if(bool[i]!=0) return false; }
return true;
}``````

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

Instead of using an integer array , you could use a boolean array and set it to true/false.

boolean [ ] bool = new bool[256];

Code will look better and make more sense.

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

public class StringEquals
{
public static void main(String args[])
{
String str = "";
String str1 = "";
if(str.length() != str1.length())
{
System.out.println("not equal");
}
else
{
int i =0;
for(;i<str.length();i++)
{
if(str.charAt(i)-str1.charAt(i) !=0)
{
break;
}
}
if(i == str.length())
{
System.out.println("equal");
}
else
{
System.out.println("not equal");
}
}
}
}

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

public class StringEquals
{
public static int isTwoStringsEqual(String s1,String s2)
{
int count = 0;
for( int i = 0 ; i < s1.length() ;i++ )
{
count = count+s1.charAt(i)^s2.charAt(i);
}
return count;
}
public static void main(String[] args)
{
String s1 = "abcd";
String s2 = "aqcd";
boolean flag = isTwoStringsEqual(s1, s2) == 0 ? true : false;
System.out.println(flag);
}
}

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

//in c
int isequal(char *a,char *b)
{
int c=strlen(a)-strlen(b);
while(a &&b)
{
c+=(*a)^(*b);
}
return c ;
}

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

//in c
int isequal(char *a,char *b)
{
int c=strlen(a)-strlen(b);
if(!(a&&b)) return c;
while((*a) &&(*b))
{
c+=(*a)^(*b);
}
return c ;
}

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

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

int main()
{
char str[10] = "hello";
char str1[10] = "hello";
int len = strlen(str);
int p = len;
int len1 = strlen(str1);
int count = 0;
int i = 0;
while(len && len1)
{
if(str[i] ^ str1[i])
break;

else
{
count++;
i++;
}
len--;
len1--;
}

printf("count = %d\n",count);

if(!(count ^ p))
printf("strings are same\n");
else
printf("not same\n");

return 0 ;

}``````

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

Vanilla C , no string functions used , plain pointers :

``````bool CompareString(char* p,char* q)
{
while(*p)
{
if((*p>*q)||(*p<*q)) return false;
p++;q++;
}
if(*p||*q) return false;
return true;
}``````

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

bool CompareString(char* p,char* q)
{
while(*p)
{
if((*p>*q)||(*p<*q)) return false;
p++;q++;
}
if(*p||*q) return false;
return true;
}

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

#include<stdio.h>
#include<conio.h>
void main()
{
String s1,s2;
int flaf=0;
clrscr();
printf("Enter string 1: ");
gets(s1);
printf("Enter string 2: ");
gets(s2);
if(s1.length()!=s2.length())
{
flag=1;
}
for(int i=0;i<s1.length();i++)
{
if((s1[i]<s2[i])||(s1[i]>s2[i]))
{
flag=1;
}
}
if(flag=1)
printf("Not equal");
else
printf("Equal");
getch();
}

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

#include<stdio.h>
#include<conio.h>
void main() {
String s1,s2;
int flaf=0;
clrscr();
printf("Enter string 1: ");
gets(s1);
printf("Enter string 2: ");
gets(s2);
if(s1.length()!=s2.length()) {
flag=1; }
for(int i=0;i<s1.length();i++) {
if((s1[i]<s2[i])||(s1[i]>s2[i])) {
flag=1; } }
if(flag=1)
printf("Not equal");
else
printf("Equal");
getch(); }

Comment hidden because of low score. Click to expand.
-1
of 1 vote

It is a Java question BTW.

Comment hidden because of low score. Click to expand.
-1
of 1 vote

you could use compareTo or intern the strings, i believe

Comment hidden because of low score. Click to expand.
-1
of 1 vote

Compare the length first, then compare each character.

Comment hidden because of low score. Click to expand.
-1
of 1 vote

``````public boolean isEquals( String str1, String str2 ){

if( str1 == str2 ){
return true;
}

if( str1 == null ){
return str2 == null ? true : false;
}

if( str1.length() != str2.length() ){
return false;
}

int res = 0;

for( int i =0; res == 0 && i < str1.length(); i++ ){
res |= str1.charAt(i) ^ str2.charAt(i);
}

return res == 0;
}``````

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

in this code you are using equal operator.

Comment hidden because of low score. Click to expand.
-1
of 1 vote

why not use the strcmp for comparing??

Comment hidden because of low score. Click to expand.
-1
of 1 vote

Let the 2 strings be s1 and s2

``````if(s1.compareTo(s2) != 0)
return false;
else
return true;``````

Comment hidden because of low score. Click to expand.
-1
of 1 vote

``````public class areStringsSame {

public static void main(String ar[]){
String a="hello";
String b="hello";
String c="hell";
System.out.println(a.hashCode());
System.out.println(b.hashCode());
System.out.println(c.hashCode());

System.out.println(areTheStringsSame(a, b));
System.out.println(areTheStringsSame(a, c));
System.out.println(areTheStringsSame(a, c));

}

public static boolean areTheStringsSame(String a,String b){
switch (getDifferenceInHashCode(a, b)) {
case 0:
return true;

default:
return false;
}
}

public static int getDifferenceInHashCode(String a,String b){
return a.hashCode() - b.hashCode();
}

}``````

Comment hidden because of low score. Click to expand.
-1
of 1 vote

C implementation

``````if(strcmp(str1, str2) != 0)
printf("Not equal");
else
printf("Equal");``````

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.

### 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.