Cracking the coding interview | Step-13 | message controlled

DAY 13: coding interview questions

Question 1:

The “Nambiar Number” Generator: M.Nambiar has devised a mechanism to process any given mobile number and thus generate a new resultant number. He calls this mechanism as the “Nambiar Number Generator” and the resultant number is referred to as the “Nambiar Number”. The mechanism is as follows “ In the given mobile number, starting with the first digit, keep on adding all subsequent digits till the state (even or odd) of the sum of the digits is opposite to the state (odd or even) of the first digit. Continue this from the subsequent digit till the last digit of the mobile number is reached. Concatenating the sums thus generated results in the Nambiar Number.”
The below examples will help to illustrate this.
Please also look at the bottom of this problem description for the expected function prototype.
Example 1
If the given mobile number is 9880127431
The first pass should start with the first digit.
First digit is 9 which is odd.
So, we will keep adding subsequent digits till the sum becomes even.
9+8=17 (17 is odd, so continue adding the digits)
9+8+8 = 25 (25 is odd, so continue adding the digits)
9+8+8+0 = 25 (25 is odd, so continue adding the digits)
9+8+8+0+1 = 26 ( 26 is even, which is opposite to the state of the first digit 9)
So, Stop first pass here and remember that the result at the end of first pass = 26
Now we enter the second pass.
The second pass should start after the digit where we stopped the first pass.
In the first pass we have added the digits 9,8,8,0 and 1.
So, the first digit for second pass will be 2, which is even.
Now, we will keep adding subsequent digits till the sum becomes odd.
2+7=9 ( 9 is odd, which is opposite to the state of the first digit 2)
So, Stop second pass here and remember that the result at the end of second pass = 9
Now we enter the third pass.
In the first pass we have added the digits 9,8,8,0 and 1, and the resultant sum was 26.
In the second pass we have added the digits 2 and 7, and the resultant sum was 9.
The third pass should start after the digit where we stopped the second pass.
So, the first digit for third pass will be 4, which is even.
Now, we will keep adding subsequent digits till the sum becomes odd.
4+3=7 ( 7 is odd, which is opposite to the state of the first digit 4)
So, Stop third pass here and remember that the result at the end of third pass = 7
Now we enter the fourth pass.
In the first pass we have added the digits 9,8,8,0 and 1, and the resultant sum was 26.
In the second pass we have added the digits 2 and 7, and the resultant sum was 9.
In the third pass we have added the digits 4 and 3, and the resultant sum was 7.
The fourth pass should start after the digit where we stopped the third pass.
So, the first digit for fourth pass will be 1, which is odd.
Now, we will keep adding subsequent digits till the sum becomes even.
However, we realize that this digit 1 is the last digit of the mobile number and there are no further digits. So, Stop fourth pass here and remember that the result at the end of fourth pass = 1

For the mobile number 9880127431, the resultant number (Nambiar Number) will be the concatenation of the results of the 4 passes = [26][9][7][1] = 26971
Note1: Please note that the number of passes required to process the given number may vary depending upon the constitution of the mobile number.
Note2: Also note that, 0 should be considered as an even number
Example 2
If the given mobile number is 9860857152
First digit 9 is odd.
First pass results in 9+8+6+0+8+5= 36
Second pass results in 7+1= 8
Third pass results in 5+2= 7
Note that the third pass stops at 7 (even though we do not meet a change of state) because we have reached the end of the mobile number.

For the mobile number 9860857152, the resultant number (Nambiar Number) will be the concatenation of the results of the 3 passes = [36][8][7] = 3687

Example 3
If the given mobile number is 8123454210
The resultant number (Nambiar Number) will be 95970
Example 4
If the given mobile number is 9900114279
The resultant number (Nambiar Number) will be 181149

//Code:

import java.io.*;
import  java.util.*;
class UserMaincode
{
public int nnGenerator(String input1)
{
String s=input1;
  int i=0,sum=0,j;
  String str="";
  String k="";

  while(i<s.length())
  {
   j=(int)s.charAt(i)-48;
   k=EvenOdd(j);
   sum=j;
   if(k.equals("odd"))
   {
    i++;
    while(EvenOdd(sum)=="odd" && i<s.length())
    {
     int m=(int)s.charAt(i)-48;
     sum=sum+m;
     i++;
    }
    str=str+sum;
    sum=0;
   }
   else if(k.equals("even"))
   {
    i++;
 
    while(EvenOdd(sum)=="even" && i<s.length())
    {
     int m=(int)s.charAt(i)-48;
     sum=sum+m;
     i++;
    }
    str=str+sum;
    sum=0;
   }
  }
  int val=Integer.parseInt(str);
  return val;
 }
 public String EvenOdd(int sum)
 {
  if(sum%2==0)
   return "even";
  else
   return "odd";
 }
}











Question 2:

Message controlled Robot movement with 90 degrees turning capability and 1 unit moving capability

Harish, an engineering student needs to submit his final year project. He decides to create a Robot which can be controlled by a set of instructions. He also decides that a grid (of X and Y axis) should be defined and the robot should move only within that grid. The set of instructions to move the robot should be given as a single message (string) and the Robot should accordingly move and reach the expected location. If the given instructions lead to a position which is out of the given grid, the Robot should stop at the last valid instruction.​

Harish decides to write a function named  moveRobot that should process the given inputs and return a string representing the final position of the Robot.   The function moveRobot will take  4 input parameters that define the size of the grid (X and Y axis), the current position of the Robot, and the message (string) containing the set of movement instructions.


The first two input parameters define the size of the grid. input1 = X axis of the grid input2 = Y axis of the grid Note that input1 and input2 will always be > 0. So, the valid grid area for the robot's movement should be the rectangular area formed between the diagonal ends (0,0) and (X,Y)


The third parameter defines the current (starting) position of the robot.

input3 = current position of the robot, represented as a string containing 3 values separated by a –  (hyphen). The format of input3 is x-y-D, where x and y represent the current (starting) position of the robot and D represents the direction where the robot is currently facing. Valid values for direction D are E, W, N, or S, representing East, West, North and South respectively.


The fourth input parameter represents the single message containing the set of instructions to move the robot.

input4 = movement Instructions to the robot, represented as a string containing the instructions separated by a space. The message will consist of a series of M, L or R, where
M means "Move 1 unit forward in the direction that the robot is facing",
L means "Turn 90 0 towards left", and 

R means "Turn 90 0 towards right". 
Output expected to be returned by the function -

The function is expected to process the given inputs and return a string representing the final position of the Robot. The returned string should be of the format x-y-D, where x and y represent the final (end) position of the robot and D represents the direction where the robot is finally facing. Valid values for direction D are E, W, N, or S, representing East, West, North and South respectively. Note that an " -ER" must be appended to the output string if the traversal finally stopped due to an invalid move (see the below two examples for more clarity on this)

Note:
You can assume the grid to be similar to the 1st quadrant of the regular graph sheet. In a regular graph sheet of dimensions x units and y units, (0,0) is the bottom left corner and (x,y) is the top right corner. Ex: For a grid of 5 x 5, the bottom left corner will be (0,0) and top right corner will be (5,5).
The starting position of the robot (third input parameter) will be any position on the grid. i.e. it need not always be (0,0)
You can assume that the current position (starting position, specified in input3) will always be a valid position within the specified grid.
IMPORTANT - Note that the instructions L and R only change the direction of the robot without moving it. The instruction M moves the robot 1 unit forward in the direction that the robot is facing.
Invalid moves should not be allowed - Any move that could lead the robot to a position beyond (outside) the defined x and y axis of the grid OR below 0 on either x or y axis, should be considered an invalid move. (see below examples to get clarity)
Example1 –

input1: x = 3

input2: y = 3

input3: 2-2-E

input4: R M L M L M

Output: The function should return 3-2-N

Explanation:

The size of the grid is 3x3 units. Current (starting) position of the robot is (2,2) facing East. After processing the set of instructions given in input4, the new position will be in (3,2) facing North. So, the function is expected to return the output in the format x-y-D i.e. 3-2-N

Example2 –

input1: x = 3

input2: y = 4

input3: 2-2-E

input4: R M L M L M R M

Output: The function should return 3-2-E-ER
Explanation:
The size of the grid is 3x4 units. Current (starting) position of the robot is (2,2) facing East. After processing the set of instructions given in input4, the new position will be in (3,2) facing East. Note that the last instruction (M) leads to a position outside the grid, so the valid moves stop at R which is the second last instruction. In this case, the function is expected to return the output representing the last valid position appended with “–ER” representing ERROR. So, the function should return the output as 3-2-E-ER
IMPORTANT NOTE: The output format should be strictly as specified above. Any extra spaces before, after or within the output string will result in failure. Also, the alphabets in the output string should be in upper-case.
NOTE: The above few examples are only to help you understand the question. The actual test-case values will be different from these, so you must ensure to check the result for all possible cases.

//Code

public class Usermaincode
{
public string moveRobot(int input1,int input2,string input3,string input4)
{
String pos[]=input3.split("-");
   String mov[]=input4.split(" ");
   int i,flag=0;
   String str="";
   for(i=0;i<mov.length;i++)
   {
    if(mov[i].equals("R"))
    {
    if(pos[2].equals("N"))
     pos[2]="E";
    else if(pos[2].equals("E"))
     pos[2]="S";
    else if(pos[2].equals("S"))
     pos[2]="W";
    else if(pos[2].equals("W"))
     pos[2]="N";
    }
    else if(mov[i].equals("L"))
    {
     if(pos[2].equals("N"))
     pos[2]="W";
    else if(pos[2].equals("E"))
     pos[2]="N";
    else if(pos[2].equals("S"))
     pos[2]="E";
    else if(pos[2].equals("W"))
     pos[2]="S";
    }
    else if(mov[i].equals("M"))
    {
     if(pos[2].equals("N"))
     {
      int k=Integer.parseInt(pos[1]);
      if(k<input2)
      {
       k++;
       pos[1]=Integer.toString(k);
      }
      else
      {
       flag=1;
       break;
      }
     }
     else if(pos[2].equals("E"))
     {
      int k=Integer.parseInt(pos[0]);
      if(k<input1)
      {
       k++;
       pos[0]=Integer.toString(k);
      }
      else
      {
       flag=1;
       break;
      }
     }
     else if(pos[2].equals("S"))
     {
      int k=Integer.parseInt(pos[1]);
      if(k>0)
      {
       k--;
       pos[1]=Integer.toString(k);
      }
      else
      {
       flag=1;
       break;
      }
     }
     else if(pos[2].equals("W"))
     {
      int k=Integer.parseInt(pos[0]);
      if(k>0)
      {
       k--;
       pos[0]=Integer.toString(k);
      }
      else
      {
       flag=1;
       break;
      }
     }
    }
   }
   str=str+pos[0];
   for(i=1;i<3;i++)
   {
    str=str+"-"+pos[i];
   }
   if(flag==1)
   {
    str=str+"-ER";
   }
   return str;
}}





Post a Comment

0 Comments