Saturday, September 12, 2015

Amazon's Interview question for Unix Shell Scripting Production support position

One of my friend recently went to attend an interview in Amazon for Unix, Shell Scripting & troubleshooting position.
Interviewer first asked him to reverse this statement which is written as
echo "I am from India";
now reverse the Statement as " India from am I";
My friend very easily able to solve this using Unix's command.

echo "I am from India" | awk 'BEGIN{ORS="";}{ for (i=NF; i>0; i--) print $i; print "\n"; }'
output is as desired: India from am I



But as soon as he gave the Unix command , Interviewer threw another question on him to reverse this same statement using Java without using the reverse function of Java. Interviewer may ask you to solve the same problem in whichever language you would have mentioned in CV or you would say you know even basic of it or if you say no for any of the programming languages, they will simply ask the logic to solve the question.
My friend had very basic knowledge of Java programming so he gave only the logic part and asked me to solve this in Java.

So here is my solution to the same question in Java.
Solving the same reversing of statement in Java.
public class Reverse {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  String message = "I am from India";    //Actual statement given
  String [] strArr;  // declaring an String array
  String rev = "";
  strArr = message.split(" ");    //now splitting the message to get one word                                                 //each in array index
  int len = strArr.length;    
  for(int i=len-1; i>=0; i--)     
  {
   rev += strArr[i]+" ";    //traverse the loop from last index and                                                   //store in a string
  
  }
  System.out.print(rev);
 }

}


No comments:

Post a Comment