Salesforce Interview Question for Quality Assurance Engineers


Team: Quality Engineering
Country: United States




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

<pre lang="" line="1" title="CodeMonkey93998" class="run-this">/* The class name doesn't have to be Main, as long as the class is not public. */
class Main
{
private static String reverse(String s) {
char temp;
int l = s.length();
char[] a = s.toCharArray();
for(int i =0;i<a.length/2;i++){
temp=a[i];
a[i]=a[l-i-1];
a[l-i-1]=temp;
}
return new String(a);
}
Output: dlroW olleH
public static void main (String[] args) throws java.lang.Exception
{
String str="Hello World"
System.out.print(reverse(str));
}
}

</pre><pre title="CodeMonkey93998" input="yes">
String str="Hello World"</pre>

- merry September 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

ublic static void main(String[] args) {
String str;
Scanner sc=new Scanner(System.in);
System.out.println("enter stirng which is to reverse");
str=sc.nextLine();
System.out.println(str);
System.out.println("reverse stirng is:");
int len=str.length();
for (int i = len-1; i >= 0; i--) {
System.out.print(str.charAt(i));

- paramjeet September 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.io.*;
class Reverse{
public static void main(String args[]) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb = new StringBuffer(br.readLine());
System.out.println(sb.reverse());
}
}

- Nagesh December 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Here's one more efficient way:

private String doReverseSmartly(String str, int length) {

int n = length - 1;
char []strArray = str.toCharArray();
for (int j = (n - 1) >> 1; j >= 0; --j) {
char temp = strArray[j];
char temp2 = strArray[n - j];

strArray[j] = temp2;
strArray[n - j] = temp;
}
return Arrays.toString(strArray);

}
}

- Narendra February 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

private String doReverseSmartly(String str, int length) {

int n = length - 1;
char []strArray = str.toCharArray();
for (int j = (n - 1) >> 1; j >= 0; --j) {
char temp = strArray[j];
char temp2 = strArray[n - j];

strArray[j] = temp2;
strArray[n - j] = temp;
}
return Arrays.toString(strArray);

}
}

- Narendra February 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String Reverse (String inArgs){
char[] charOrig = inArgs.toCharArray();
char temp;
for (int i= 0; i< inArgs.length()/2; i++) {
temp = charOrig[i];
charOrig [i] = charOrig [inArgs.length ()-i-1];
charOrig [inArgs.length ()-i-1] = temp;
}
return String.copyValueOf(charOrig);
}

- Jason September 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Code in c:

char * strrev(char *str)
{
	if (str==null)
	return null;
        int i,l=0;
//measuring length of string
	while(str[l]!='\0')
		 l++;
//swaping first nth an last nth till middle of string 
	for(i=0;i!=l/2;i++){
//swaping first and last nth char using xor operation
		str[i]=str[l-i-1]^str[i];
		str[l-i-1]=str[l-i-1]^str[i];
		str[i]=str[l-i-1]^str[i];
	}
}

- Suman February 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

forgot to return in end

- Anonymous February 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void reverse(char *str)
{
        if(*str)
        {
                reverse(str+1);
                putchar(*str);
        }
}

- Ganesh February 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A neater implementation can be to use Stack since this is clearly the case of LIFO (Last in First out). I will explain using 'Java'

input : 'Hello World'
output : 'dlroW olleH'

Clearly, this is a first in, last out.
'H' is first in the original string, last in the output string.
'e' is second in the original string, second last in the output string.
Hence, a perfect place to use stacks.

Steps:
1. Read character one by one from left of the string and push it into the stack until you reach end of the string.
2. While stack is not empty, pop character from the stack and add it to the output.

Classes and methods:
Stack.java                        //Data structure for stack
StringReversal.java          //Utility class to reverse a string
Tests.java //Junit Test class
-------------
Stack.java
-------------

public class Stack {
	int maxSize;
	int top;
	char[] stack;

	Stack(int size){
		maxSize = size;
		top=-1;
		stack = new char[maxSize];
	}
	
	public void push(char c){
		if(isFull()){
			System.out.println("Can't push. Array is full");
			return;
		}
		stack[++top]=c;
	}
	
	public char pop(){
		if(isEmpty()){
			System.out.println("Can't pop. Array is empty");
			return '\n';
		}
		return stack[top--];
	}
	
	public boolean isEmpty(){
		return (top==-1);
	}
	
	public boolean isFull(){
		return(top==maxSize-1);
	}
}
------------------------
StringReversal.java
------------------------
public class StringReversal {
	public static String reverse(String str) throws NullPointerException{
		if(str==null){
			throw new NullPointerException("str cannot be null");
		}
		Stack stack = new Stack(str.length());
		for(char c:str.toCharArray()){
			stack.push(c);
		}
		StringBuffer sb = new StringBuffer();
		while(!stack.isEmpty()){
			sb.append(stack.pop());
		}
		return sb.toString();
	}
}
-------------
Tests.java
-------------
public class StringReversalTests {
	@Test
	public void testPushPop() {
		Stack stack = new Stack(5);
		stack.push('a');
		Assert.assertEquals('a', stack.pop());
	}

	@Test
	public void testIsEmpty() {
		Stack stack = new Stack(5);
		//stack should be empty now
		Assert.assertTrue(stack.isEmpty());
		//Fill some items
		stack.push('a');
		stack.push('b');
		//isEmpty should return false now
		Assert.assertFalse(stack.isEmpty());
	}
	
	@Test
	public void testIsFull(){
		Stack stack = new Stack(5);
		//isFull should return false now
		Assert.assertFalse(stack.isFull());
		//Fill some items
		stack.push('a');
		stack.push('b');
		//isFull should return false even now
		Assert.assertFalse(stack.isFull());
		//fill the entire stack
		stack.push('c');
		stack.push('d');
		stack.push('e');
		//Stack is full, assert it is
		Assert.assertTrue(stack.isFull());		
	}
	
	@Test
	public void testReverse(){
		Assert.assertEquals("dlroW olleH", StringReversal.reverse("Hello World"));
		//Test for empty string
		Assert.assertEquals("",StringReversal.reverse(""));
		//Test for null string
		boolean gotNPE=false;
		try{
			StringReversal.reverse(null);
		} catch (NullPointerException npe){
			gotNPE = true;
		}
		Assert.assertTrue(gotNPE);
	}

	//TODO : Add more test cases for completeness
}

- Peanut March 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Mr. Peanut : Neat

- SS7 August 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
		
		char[] array = {'H','e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'};
		//char[] processed = new char[' '];
		
		for(int i = array.length-1; i>=0; i--){
			System.out.print(array[i]);
		}

- Illinois October 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

var str = "Hello World";

str = str.split("").reverse().join("");
console.log(str); // dlroW olleH

function rev(str) {
    var result = "";    
    for(var i=str.length-1; i>=0; i--) {
        result+= str[i];
    }
    return result;
}
rev(str); // dlroW olleH

- Anbu June 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think some cases to consider in mind while testing this would be
-> an empty string
-> small or large characters in a word
-> some alphanumeric characters or no.s could be there in string
->spaces present or not

- ANKIT YADAV December 22, 2016 | 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