Labs247 Interview Question for Quality Assurance Engineers


Country: United States
Interview Type: In-Person




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

private static void reverse(char[] c) {
		reverse(c,0,c.length-1);
		
		int wordStart=0;
		for(int i=0;i<c.length;i++)
		{
			if(c[i]==' ')
			{
				reverse(c,wordStart,i-1);
				wordStart=i+1;
			}
			else if(i==c.length-1)
			{
				reverse(c,wordStart,i);
			}
		}
	}

	private static void reverse(char[] c, int i, int j) {
		if(i>=j)
			return;
		for(int k=i;k<=(i+j)/2;k++)
		{
			char temp=c[k];
			c[k]=c[i+j-k];
			c[i+j-k]=temp;
		}
	}

- Vir Pratap Uttam May 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
4
of 6 vote

public class ReverseString{

        public static void main(String[] arg)
        {
                String str = "hello my name is";
                String[] arr = str.split(" ");

                for(int i=(arr.length-1); i>=0; i--)
                {
                        System.out.print(arr[i]);
                        System.out.print(" ");
                }
        }
}

- puja July 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is the simplest way to do it. I am not sure why all the above answers are so complex.

- Nani July 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package results;

import java.util.*;


public class Main {
   
Stack st;
StringTokenizer strToken;

Main(){
    
    st = new Stack();
    
}

public void reverseSentence(String str){
    
    strToken = new StringTokenizer(str);
    int count = strToken.countTokens();
    while (strToken.hasMoreTokens()){
        st.push(strToken.nextToken());
    }
    
    for ( int i =0; i < count; i++){
        System.out.println(st.pop());
    }
    
}
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Main o = new Main();
        o.reverseSentence("Hello my name is");
    }

}

As this interview was in Java, I have given solution using string tokenizer. It tokenizes string into words, we push them on a stack and then we pop all the words (tokens ) out.

- coolsolution June 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If you want to watch the video about how to reverse the words of a string or sentence please visit the link
youtu.be/WCaigw6Bz1M

- Anonymous June 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

#include<stdio.h>
 
/* function prototype for utility function to
  reverse a string from begin to end  */
void reverse(char *begin, char *end);
 
/*Function to reverse words*/
void reverseWords(char *s)
{
  char *word_begin = s;
  char *temp = s; /* temp is for word boundry */
 
  /*STEP 1 of the above algorithm */
  while( *temp )
  {
    temp++;
    if (*temp == '\0')
    {
      reverse(word_begin, temp-1);
    }
    else if(*temp == ' ')
    {
      reverse(word_begin, temp-1);
      word_begin = temp+1;
    }
  } /* End of while */
 
   /*STEP 2 of the above algorithm */
  reverse(s, temp-1);
}
 
/* UTILITY FUNCTIONS */
/*Function to reverse any sequence starting with pointer
  begin and ending with pointer end  */
void reverse(char *begin, char *end)
{
  char temp;
  while (begin < end)
  {
    temp = *begin;
    *begin++ = *end;
    *end-- = temp;
  }
}
 
/* Driver function to test above functions */
int main()
{
  char s[] = "i like this program very much";
  char *temp = s;
  reverseWords(s);
  printf("%s", s);
  getchar();
  return 0;
}
Time Complexity: O(n)

- saurabh June 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

/* reverse a string. "hello my name is" to be displayed as "is name my hello"    */

//          WORKING CODE        ||  RECURSIVE

#include<stdio.h>
#include<string.h>
int reverse( char C[25])
{
    int i;
    i=strlen(C);
    if(!i)
        return 0;
    for(i=i-1;i>=0&&C[i]!=' ';i--);
    if(i>=0)
        C[i]=NULL;
    printf("%s ",C+i+1);
    if(i>=0)
        reverse(C);
return 0;
}
int main()
{
    char S[25];
    gets(S);
    reverse(S);
return 0;
}

- niraj.nijju June 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

have an array of strings,store each word in consecutive position and finally concatenate in reverse order.

- Avenger June 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Another better method is first reverse the complete sentence and then tokenzie the reverse string and reverse the tokens. Let me explain with examples
hello my name is
1. reverse whole sentence
si eman ym olleh
2. tokenize this reverse string and then reverse the tokens in the same order of words
si=is eman = name ym= my olleh= hello
hello my name is

public void reverseSentence2(String str){
    
    char rev[]= new char[str.length()];
    for ( int i =0; i < str.length(); i++){
        rev[str.length()-i-1]=str.charAt(i);
    }
    
    String revStr = new String(rev);
    StringTokenizer token = new StringTokenizer(revStr);
    while ( token.hasMoreTokens()){
        
        String temp = token.nextToken();
        for ( int i = temp.length()-1; i>=0; i--){
            System.out.printf("%c", temp.charAt(i));
        }
        System.out.println(" ");
    }
}

- coolsolution June 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If you want to watch a wonderful video about how to reverse the words of a string visit the following link
youtu.be/WCaigw6Bz1M

- Anonymous June 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

3 methods
method 1)insert all words in stack and pop;
method 2)store words in linklist reverse link list
method 3) use recursion

- rashmi sampath June 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>

/* function prototype for utility function to
reverse a string from begin to end */
void reverse(char *begin, char *end);

/*Function to reverse words*/
void reverseWords(char *s)
{
char *word_begin = s;
char *temp = s; /* temp is for word boundry */

/*STEP 1 of the above algorithm */
while( *temp )
{
temp++;
if (*temp == '\0')
{
reverse(word_begin, temp-1);
}
else if(*temp == ' ')
{
reverse(word_begin, temp-1);
word_begin = temp+1;
}
} /* End of while */

/*STEP 2 of the above algorithm */
reverse(s, temp-1);
}

/* UTILITY FUNCTIONS */
/*Function to reverse any sequence starting with pointer
begin and ending with pointer end */
void reverse(char *begin, char *end)
{
char temp;
while (begin < end)
{
temp = *begin;
*begin++ = *end;
*end-- = temp;
}
}

/* Driver function to test above functions */
int main()
{
char s[] = "i like this program very much";
char *temp = s;
reverseWords(s);
printf("%s", s);
getchar();
return 0;
}
Time Complexity: O(n)

- saurabh June 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

string ReverseWords(char* str)
{
        if (strlen(str) <= 0) return NULL;
        char *temp = new char[strlen(str) + 1];
        strcpy(temp, str);
        stack <char*> s;
        cout << "1" << endl;

        char *tok = strtok(temp, " ");
        cout << "1" << endl;
        while (tok != NULL)
        {
                s.push(tok);
                s.push(" ");
                cout << tok << endl;
                tok = strtok(NULL, " ");
        }

        s.pop();
        // Now get back the reverser string
        string revStr;
        while (s.size() != 0)
        {
                revStr += s.top();
                s.pop();
        }

        delete temp;
        return revStr;
}

- Victor June 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

split the words and put it in a stack and the pop it one by one and construct the new string

- harish July 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In java using String and array only.

public static void main(String[] args) {
// TODO code application logic here
String str = "hello my name is";
String str1="";
int count =0;
String[] reverse = new String[10];

for(int i=0;i<str.length();i++){
if(str.charAt(i)==' ')
count++;
}
int c = count;
for(int i=0;i<str.length();i++){
if(str.charAt(i)!=' '){
str1 = str1+str.charAt(i);
}
if(str.charAt(i)==' '){
reverse[--count] = str1;
str1="";
}
}
for(int i=0;i<c;i++)
str1 = str1.concat(" "+reverse[i]);

System.out.println(str1);
}

- syed talibuddin saifi July 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Best Solution will be like this....

char *Reverse( char *str)
{
int end = strlen(str) - 1;
int start = 0;
while(start < end)
{
str[start] ^ = str[end];
str[end] ^ = str[start];
str[start] ^ = str[end];

start ++;
end --;
}
return str;
}

- Samaresh July 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*;
public class rev
{
Stack stk;

rev()
{
stk=new Stack();
}

public void reverseStr(String s)
{
int k=0;
String r="";
char arr[]=s.toCharArray();
for(int i=0;i<arr.length;i++)
{
if(arr[i]!=' ')
{
String x=arr[i]+"";
r=r.concat(x);
}

else
{
stk.push(r);
r="";
}

}
stk.push(r);
while(!stk.isEmpty())
{
System.out.print(stk.pop() +" ");
}
}

public static void main(String arg[])
{
rev r=new rev();
r.reverseStr("my name is sweta");
}}

- sweta July 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

u can also use d split function

- sweta July 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@sweta
your solution is wrong. Try to solve this without using any extra buffer and any other external functions / data structures.

- xyz July 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String str1 = " Hi my name is";
Stack stak = new Stack();
StringTokenizer st = new StringTokenizer(str1); String key ="";
while(st.hasMoreTokens())
{
key = st.nextToken();
stak.add(key);

}

while(!stak.isEmpty())
{System.out.print(stak.pop() +" ");}

- Deepshikha July 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class reverseString {

	public static void main(String[] args) {
		String ab="hello my name is";
        String [] rever=ab.split(" ");
       
        String ans="";
        for(int i=(rever.length-1);i>=0;i--)
        {
        	if(i!=3)
        	{
        		ans=ans+" "+rever[i];
        	}
        	else
        	{
        		ans=rever[i];
        	}
        }
        System.out.println(ans);
	}

}

- vikash August 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ReverseString {

	private static final String TOKEN = " ";

	public static void main(String[] args) {
		String input = "Fast and Furious";
		String[] strings = input.split(TOKEN);

		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < strings.length; i++) {
			sb.append(strings[strings.length - i - 1]).append(TOKEN);
		}
		System.out.println(sb.substring(0, sb.length() - 1));
	}
}

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

public static void main(String args[]){

String s = "hello my name is";

String[] l = s.split(" ");

StringBuffer sathi = new StringBuffer();
for(int i=l.length-1;i>=0;i--){

sathi.append(l[i]);
sathi.append(" ");

}

System.out.println(sathi.toString());




}

- Sathishwaran January 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class StringRevTest {
	public static void main(String[] args) {
		

String str="hello my name is";
String strArray[]=str.split(" ");
int len=strArray.length;
len=len-1;
System.out.println(strArray.length);
for(int i=len;i>=0;i--)
{
	System.out.print(strArray[i]+" ");
	
}
	}

}

- Dharmendra Kumar(Hilsa/Patna/Sastra-TN/Bangalore) February 18, 2013 | 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