Oracle Interview Question for Software Engineer / Developers






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

Here is my code in java (take from k2code.blogspot.in/2011/12/how-will-you-check-if-s1-is-rotated.html) :

boolean isRotation(String s1,String s2) {
    return (s1.length() == s2.length()) && ((s1+s1).indexOf(s2) != -1);
}

- kinshuk.ram May 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

perfect
1.) we first compare length so that we will not get true result for "waterbottle" and "water".
2.) we add the 2 rotated strings in question so "erbottlewat" + "erbottlewat" = "erbottlewaterbottlewat". you will see that the original string "waterbottle" is substring of the string formed by adding 2 rotated strings.

- Algorithmy October 23, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

//Assume you have a method isSubstring which checks if one word is a substring of another.
    //	Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to isSubstring i.e., waterbottle is a rotation of erbottlewat
	// COMPLEXITY O ( n^2 ) + O (n + m) = O ( n^2 )
	public static boolean isRotation(char str1 [], char str2[] )
	{
		return isSubstring(append(str2,str2),str1);
	}
	// COMPLEXITY  O ( n + m )
	public static char[] append(char str1[], char str2[]){
		char result[] = new char[str1.length + str2.length];
		int index = 0;
		int len1 = str1.length; 
		while(index < str1.length)
			result[index] = str1[index++];
		index = 0;
		while(index < str2.length)
			result[len1 + index] = str2[index++];
		
		return result;
	}
	// COMPLEXITY O ( N^2 )
	public static boolean isSubstring(char str1[], char str2[]){
		int counter = 0;
		for(int i = 0; i < str2.length; i++){
			int start = i;
			while(counter < str2.length && start < str1.length && str1[start++] == str2[counter++]){
			    if(counter == str2.length)return true;
			}
			counter = 0;
		}
		return false;
	}

- .·´¯`·.´¯`·.¸¸.·´¯`·.¸><(((º> January 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about this?

isSubstring (s2+s2, s1)

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

I think this is a concatenation problem.
Concatenate the input string s1 with itself and check if s2 is contained in s1.concat(s1).

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

public boolean isRoatate(String s1, String s2){
s1+=s1;
return isSubstring (s1,s2);
}

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

public boolean isRoatate(String s1, String s2){
    	s1+=s1;
    	return isSubstring (s1,s2);

}

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

You have to check that s1.size() == s2.size() too, otherwise s1 = waterbottle, s2 = water will return true.

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

public static boolean rotation_check(String str1, String str2){

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

return (str1+str1).replace(str2,"").equals(str1);

}

- Parthipan February 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Amazing solution, can you please explain why this solution is correct?

- Arpit August 10, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public class rotationstring {

public static boolean rotation_check(String str1, String str2){
String temp = str1+str1;

if(str1.length() != str2.length())
return false;
if(temp.toLowerCase().contains(str2.toLowerCase()))
return true;

return false;
}

public static void main(String[] args){
String st1 = "waterbottle";
String st2 = "erbottlewat";
System.out.println(rotation_check(st1, st2));
}

}

- Ganesh1238 January 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

again ganesh?? your code does not work!!!!!!!!!!!!!!!!!!!!!!

- .·´¯`·.´¯`·.¸¸.·´¯`·.¸><(((º> January 26, 2014 | 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