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;
}

- sjain October 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- pradegup October 25, 2012 | Flag
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;
}

- sahaj October 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

why need to have a extra space..?

- sjain October 25, 2012 | Flag
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.

- rahul December 10, 2012 | Flag Reply
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;
	}

- pradegup October 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Buzz_Lightyear November 14, 2012 | Flag
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");
}
}
}
}

- Raghava October 25, 2012 | Flag Reply
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);
}
}

- Anonymous November 24, 2012 | Flag Reply
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 ;
}

- Anonymous August 23, 2013 | Flag Reply
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 ;
}

- Anonymous August 23, 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 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 ;
  	   
  }

- naveen.80546 October 22, 2014 | Flag Reply
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;
}

- Lenin October 04, 2015 | Flag Reply
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;
}

- Lenin October 04, 2015 | Flag Reply
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();
}

- Subramaniyan Venkatraman February 12, 2017 | Flag Reply
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(); }

- Subramaniyan Venkatraman February 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

It is a Java question BTW.

- fz October 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

you could use compareTo or intern the strings, i believe

- some guy with a haircut October 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Compare the length first, then compare each character.

- Anonymous October 26, 2012 | Flag Reply
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;
	}

- m@}{ October 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

in this code you are using equal operator.

- sjain October 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

why not use the strcmp for comparing??

- ps November 06, 2012 | Flag Reply
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;

- Vishal December 29, 2012 | Flag Reply
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();
	}

}

- Kaushik December 16, 2013 | Flag Reply
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");

- Vipul Bansal September 26, 2014 | 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