Amazon Interview Question
Quality Assurance EngineersTeam: Kindle
Country: India
Interview Type: In-Person
I am not sure why this is downvoted, but this is the most efficient solution for the problem
Anon used SPLIT which means that this functionality is supported by the language he was using (ex- Java). Therefore, if SPLIT is available just use it to obtain the array with different words and print the array in reverse.
If,however, the function SPLIT is not defined for a language than this might be the optimum solution, but that isnt the case here since point 3 clearly uses SPLIT.
we can create our own split easily
essentially brute force split is as good as language library split
private static String revereseWords(String inputString,char spaceChar){
char[] charArray = inputString.toCharArray();
int totalLength = charArray.length;
char[] destArray = new char[totalLength];
int spaceCount = 0;
for (int i = totalLength , length = 0; i > 0; i--,length++) {
if (charArray[i-1] != spaceChar){
spaceCount ++;
}else{
System.arraycopy(charArray, i, destArray, length-spaceCount, spaceCount);
destArray[length] = ' ';
spaceCount = 0;
}
}
if (spaceCount > 0){
System.arraycopy(charArray, 0, destArray, totalLength-spaceCount, spaceCount);
}
return String.valueOf(destArray);
}
My Solution without Split to avoid regex cost
System.out.println(revereseWords("This is test",' '));
Without use of any array split....
static void ReverseString(String ReverseString)
{
int endPrint = ReverseString.Length-1;
for (int i = ReverseString.Length-1; i >= 0; i--)
{
if (ReverseString[i] == ' ' || i == 0)
{
for (int j = i; j <= endPrint; j++)
{
Console.Write(ReverseString[j]);
}
endPrint = i;
Console.Write(" ");
}
}
}
Heres the solution in C without using stack
#include<stdio.h>
int main(){
char string[] = " Hello i am shashank and how are you man!!";
int i=0;
int j=0;
while(string[i] != '\0')
i++;
i--;
while(i>=0){
if(string[i] == ' ' || i==0){
j=i+1;
while(string[j] != ' ' && string[j] != '\0')
printf("%c",string[j++]);
printf(" ");
}
i--;
}
}
Here my C++ version:
string get_words_reverse(string &s) {
int j = s.size() - 1;
for(int i = 0; i < s.size()/2.0; i++) {
swap(s[i], s[j]);
j--;
}
for(int i = 0; i <= s.size(); i++) {
if(s[i] == ' ' || i == s.size()) {
int end = i - 1;
int len = end - start;
for(int j = 0; j <= len/2.0; j++) {
swap(s[start], s[end]);
start++;
end--;
}
start = i + 1;
}
}
return s;
}
public void check()
{
index=0;
for(int i=0;i<input.length();i++)
{
if(i<(input.length()-1))
{
index=input.indexOf(" ");
if(index!=-1)
{
output= input.substring(0,index)+" "+output;
input=input.substring(index+1);
i=0;
}
}
else
{
output= input.substring(0)+" "+output;
}
}
System.out.println(output);
}
import java.util.Stack;
public class PrintLineInReverse {
public static String reverseString(String str){
Stack<String> sk = new Stack<String>();
StringBuilder sb = new StringBuilder();
int lasti=0; boolean flag=false;
String st;
for(int i=0;i<str.length();i++){
if(str.charAt(i)==' '){
st = str.substring(lasti, i);
sk.push(st);
flag = true;
continue;
}
if(flag){
flag = false;
lasti = i;
}
}
st = str.substring(lasti, str.length());
sk.push(st);
while(!sk.isEmpty()){
sb.append(sk.pop()+" ");
}
return sb.toString();
}
public static void main(String[] args) {
String str = "This is test sdfsd";
System.out.println(reverseString(str));
}
}
Here is the C++ code:
int main() {
string cszStr = "I am tester trying for new opportunities";
int len = cszStr.length();
for (int i=0,x=0; i<= len; i++, x++)
{
if (32 == (int)cszStr[len-i] || i == len)
{
for (int y = len-i; y <= ((len-i)+x); y++)
cout << cszStr[y];
x = 0;
}
}
return 0;
}
package programs;
public class StringSplit {
public static void main(String[] args)
{
String str="This is Test";
String[] arr=str.split(" " );//splits the array by space
System.out.println(arr.length);
System.out.println("" +str);
for(int i=arr.length-1;i>=0;i--)
{
System.out.print(" "+arr[i]);
}
}
}
package programs;
public class StringSplit {
public static void main(String[] args)
{
String str="This is Test";
String[] arr=str.split(" " );//splits the array by space
System.out.println(arr.length);
System.out.println("" +str);
for(int i=arr.length-1;i>=0;i--)
{
System.out.print(" "+arr[i]);
}
}
}
This is my C++ version, O(n) complexity and O(1) space (in-place). First reverses the entire string. Then reverse each word separated by one or more spaces.
#include <iostream>
void my_reverse(std::string& str, size_t begin, size_t end)
{
while (begin < end)
std::swap(str[begin++], str[end--]);
}
std::string reverse_words(std::string& phrase)
{
// Reverse the entire string
my_reverse(phrase, 0, phrase.size() - 1);
// Reverse each word
size_t begin_pos = 0;
while (begin_pos != std::string::npos) {
size_t end_pos = phrase.find(' ', begin_pos);
if (end_pos == std::string::npos)
end_pos = phrase.length();
my_reverse(phrase, begin_pos, end_pos - 1);
begin_pos = phrase.find_first_not_of(' ', end_pos + 1);
}
return phrase;
}
int main() {
std::string str = "This is a test";
std::cout << reverse_words(str) << std::endl;
return 0;
}
this is my CPP code working fine with few test.
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
string stringRev(string & Str,int len)
{
string arr[len];
int i=0,j=0;
int count =0;
while(Str[i]!='\0')
{
if(Str[i]==' ')
{
arr[j]= Str.substr(count,i-count);
cout<<Str.substr(count,i-count)<<endl;
j++;
i++;
count=i;
}
else
{
i++;
}
}
arr[j]= Str.substr(count,i-count);
cout<<Str.substr(count,i-count)<<endl;
string strnew;
for(int k=len-1;k>=0;k--)
{
strnew.append(arr[k]);
strnew.append(" ");
}
cout<<strnew<<endl;
return strnew;
}
int no_of_space(string &str)
{
int len=0;
int i=0;
while(str[i]!= '\0')
{
if (str[i]==' ')
{
len++;
}
i++;
}
len++;
cout<<"Length :"<<len<<endl;
return len;
}
int main(int argc, char *argv[])
{
string str="Priyanshu is wrong what about this question";
int len = no_of_space(str);
string rtn = stringRev(str,len);
cout<<rtn<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
1.Make a function to reverse the string passed:: String Reverse(String s){}
- Anonymous November 23, 20132.Reverse whole string.:: s=Reverse(s);
3.Split The String by space. a[] = s.Split(" ");
3.For each word in a, Reverse and print i.e:: a[i] = Reverse(a[i]) then print a[i].