Oracle Interview Question for Software Engineer in Tests


Country: India
Interview Type: In-Person




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

Here is the code...its not readable but let me know if this is ok..This will replace all occcurence of specified string if both are of same length. I will make the code readable if logic seems fine to you.

public String replace(String str,String strreplace,String replacewith) throws Exception{

char[] charStr=str.toCharArray();
char charreplace[]=strreplace.toCharArray();
char replacewit[]=replacewith.toCharArray();
if(strreplace.length()!=replacewith.length()){
throw new Exception();
}else{
if(strreplace.length()>str.length()){
throw new Exception();
}else{
int[] a=new int[charreplace.length];
int k=0;
for(int i=0;i<charStr.length;i++){
for(int j=0;j<charreplace.length;j++,i++){
if(charStr[i]==charreplace[j]){
k++;
a[j]=i;
continue;
}else{
k=0;
break;
}

}
if(k==charreplace.length){
for(int m=0;m<a.length;m++){
charStr[a[m]]=replacewit[m];
System.out.print(a[m]+" ");
}
}

}
}}
return new String(charStr);


}

- anuj71307 May 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static void replaceData(String userInput, String[] datatoReplace) {
String[] userArr = userInput.split(" ");
for(int i = 0; i < userArr.length ; i++) {
if(userArr[i].equals(datatoReplace[0])){
finalOutput.append(datatoReplace[1]);
finalOutput.append(" ");
}
else {
finalOutput.append(userArr[i]);
finalOutput.append(" ");
}

}

// datatoreplace array with oldword,newword.
Please review my code and provide any better solution.

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

public class ReplaceSubString{
	static String s = "kanha is a good boy & he is the best";
	static int count,i;
	static char[] ch;
	static char[]ch1 = new char[100];
	static void replace(String ss,String sou,String dest)
	{ 
	    ch =s.toCharArray();	
	    int jj=0;
	   	for(int j =0;j<ch.length;j++)
			{  
	   		ch1[jj++]=ch[j];
	   		     count =0;
			     i=0;
				  if(sou.charAt(i)==ch[j])
				  {    
                                        count++;      
					int jjj=j;
                                     while(i<sou.length()-1)
					  {
						  if(sou.charAt(++i)==ch[++jjj])
							  count++;
					  }
				     if(count == sou.length())
				    	 {
				    	 int kk=0;
                                       j=jjj; 
		                 int ptr = --jj;
				    	for(int iii=ptr;iii<ptr+dest.length();iii++)
				    	{
				    	   ch1[iii] = dest.charAt(kk++);
				    	   jj++;
				    	}
				    	   
				    } 
				   }
			}
	}
	public static void main(String[] args) throws Exception
	{ 
		replace(s,"kanha","kanhaiya");
		for(char chh:ch1)
		System.out.print(chh);	
	}
}

- Kanha August 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Above is nice but it fails when "s" is replace with "kanha is a great good boy & he is the best".

P.S. Dont go into meaninf of sentence .what I mean here is if there is another partial matching string then that is also getting replace till the length of to be replace string.

Thanks.

- Anuj August 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is just another solution to the same problem.

public class StringPractice {

public static void main(String[] args) {

String originalStr = "prasanthi and madhu are one and the same";
String replaceThis = "madhu";
String replaceWith = "madhumita";

String finalStr = replaceSubstring(originalStr, replaceThis,
replaceWith);
System.out.println(originalStr);
System.out.println(finalStr);

}

public static String replaceSubstring(String originalStr,
String replaceThis, String replaceWith) {
String finalStr = "";

char[] original = originalStr.toCharArray();
char[] replaceThisArr = replaceThis.toCharArray();
int tbrLength = replaceThis.length();

for (int i = 0; i < original.length; i++) {
if (original[i] == replaceThisArr[0]
&& original.length - i >= tbrLength) {
String tempStr = "";
for (int j = 0; j < tbrLength; j++) {
tempStr += original[i + j];
}
if (tempStr.equals(replaceThis)) {
finalStr += replaceWith;
i = i + tbrLength - 1;
} else {
finalStr += original[i];
}
} else {
finalStr += original[i];
}
}

return finalStr;
}

}

- Prasanthi Devarasetti October 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package example;

public class Test {
public static void main(String[] args) {
String input = "abcdecdfg";
String oldSubStr = "abcdecdfgs";
String newSubStnr = "**";
String result = replace(input, oldSubStr, newSubStnr);
System.out.println(result);
}

private static String replace(String input, String oldSubStr,
String newSubStr) {
String result = null;
if (input.contains(oldSubStr)) {
String[] arr = input.split(oldSubStr);
if (arr.length != 0) {
result = arr[0];
for (int j = 1; j < arr.length; j++) {
result = result + newSubStr + arr[j];
}
}else{
result=newSubStr;
}

} else {
result = input;
}
return result;
}
}

- saroj.mandal1989 November 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String args[])
{
Scanner in = new Scanner(System.in);
StringBuffer sb = new StringBuffer();

System.out.println("Enter the actual string");
String s1 = in.nextLine(); // Actual String
System.out.println("Enter the string to be replaced");
String s2 = in.nextLine(); // String to be replaced
System.out.println("Enter the replacing string");
String s3 = in.nextLine(); // Replacing string

String start;
String end;

while(s1.indexOf(s2) != -1)
{
int k = s1.indexOf(s2);
int p = k+s2.length();
start = s1.substring(0,k);
end = s1.substring(p,s1.length());
s1 = start+s3+end;
}

System.out.println("The string after all replacements = "+s1);

}

- pal April 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

String replace(String str, char oldc, char newc){
	char[] starr = str.toCharArray();
	
	for(int i=0; str[i]!='\0'; i++)
		if(starr[i]==oldc)
			starr[i] = newc;

	return new String(starr);
}

- Ashish May 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what if substring need to be replace?

- sjain May 21, 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