TCS Coding Question 1 | Our hoary culture had several great persons ….

Problem Statement

Our hoary culture had several great persons since time immemorial and king vikramaditya’s nava ratnas (nine gems) belongs to this ilk.They are named in the following shloka:

Among these, Varahamihira was an astrologer of eminence and his book Brihat Jataak is recokened as the ultimate authority in astrology.

He was once talking with Amarasimha,another gem among the nava ratnas and the author of Sanskrit thesaurus, Amarakosha.

Amarasimha wanted to know the final position of a person, who starts from the origin 0 0 and travels per following scheme.

TCS NQT Coding

Scheme

  • He first turns and travels 10 units of distance
  • His second turn is upward for 20 units
  • Third turn is to the left for 30 units
  • Fourth turn is the downward for 40 units
  • Fifth turn is to the right(again) for 50 units

… And thus he travels, every time increasing the travel distance by 10 units.

Test Cases

Case 1

  • Input : 3
  • Expected Output :-20 20

Case 2

  • Input: 4
  • Expected Output: -20 -20

Case 3

  • Input : 5
  • Expected Output : 30 -20

Case 4

  • Input : 7
  • Expected Output : 90 -20
Our hoary culture had several great persons since time (1)

31 comments on “TCS Coding Question 1 | Our hoary culture had several great persons ….”


  • Navin

    /*********** Using Formula for each quadrant and point *********************/
    #include
    using namespace std;
    int main()
    {
    int p,x,y;
    cin>>p;
    if((p+3)%4 == 0)
    {
    x = (p/2+1)*10;
    y = -(x-10);
    }
    else if((p+2)%4 == 0)
    {
    x = (p/2)*10;
    y = x+10;
    }
    else if((p+1)%4 == 0)
    {
    x = -((p/2)*10 + 10);
    y = -x;
    }
    else
    {
    x = -(((p-1)/2)*10 + 10);
    y = x;
    }
    cout <<x<<" "<<y;
    return 0;
    }


  • Sudeshna

    Is the case 4 shown here is representing the wrong answer?
    By the way i wrote the following code, please tell if it is correct or not
    #include
    #include

    int main()
    {
    int step, steppower = 10, x = 0, y = 0, j = 2;
    printf(“Enter the number of steps : “);
    scanf(“%d”, &step);
    if (step != 0)
    {
    for (int i = 1; i <= step; i++)
    {
    if (i%2 != 0)
    {
    x = x + pow(-1, j)*steppower;
    }
    else if (i%2 == 0)
    {
    y = y + pow(-1, j)*steppower;
    j++;
    }
    steppower = steppower + 10;
    }
    printf("The position for %d is = %d and y = %d", step, x, y);
    }
    else {
    printf("The position for %d is x = %d and y = %d", step, x, y);
    }
    return 0;
    }


  • Ashutosh

    java code is wrong , And Not able to find Target..
    cmperr
    Main.java:20: error: illegal start of expression
    public static void getDistance(int a) {
    ^
    Main.java:20: error: illegal start of expression
    public static void getDistance(int a) {
    ^
    Main.java:20: error: ‘;’ expected
    public static void getDistance(int a) {
    ^
    Main.java:20: error: ‘.class’ expected
    public static void getDistance(int a) {
    ^
    Main.java:20: error: ‘;’ expected
    public static void getDistance(int a) {
    ^
    Main.java:65: error: reached end of file while parsing
    }
    ^
    6 errors


  • Sagnik

    I’ve have solved it in this way, quite similar to the solution but have not used menu driven functions..

    import java.util.*;
    public class move
    {
    public static void main(String[] args)
    {
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int x=0,y=0,a=10,count=0;
    char ch=’s’;
    while(true)
    {
    if(ch==’s’)
    {
    x=x+a;
    a=a+10;
    ch=’u’;
    count++;
    }
    if(count==n)
    {
    break;
    }
    if(ch==’u’)
    {
    y=y+a;
    a=a+10;
    ch=’l’;
    count++;
    }
    if(count==n)
    {
    break;
    }
    if(ch==’l’)
    {
    x=x-a;
    a=a+10;
    ch=’d’;
    count++;
    }
    if(count==n)
    {
    break;
    }
    if(ch==’d’)
    {
    y=y-a;
    a=a+10;
    ch=’s’;
    count++;
    }
    if(count==n)
    {
    break;
    }
    if(ch==’s’)
    {
    x=x+a;
    a=a+10;
    count++;
    }
    if(count==n)
    {
    break;
    }
    }
    System.out.println(x+” “+y);
    }
    }


  • Man of

    #include
    using namespace std;

    pair astrology(int n){
    pair x;
    x.first=0;
    x.second=0;
    int flag=0;
    for(int i=1;i>n;
    pair pos=astrology(n);
    cout<<pos.first<<" "<<pos.second<<endl;
    return 0;
    }


  • Satya Sai Srija

    i think test case for 7 as input has got wrong result given
    1 -> right -> positive coordinate addition to x axis -> (0+10,0) -> (10,0)
    2 -> up -> positive coordinate addition to y axis -> (10,0+20) -> (10,20 )
    3 ->left -> negative coordinate addition -> (10-30,20) -> (-20,20)
    4 -> down -> negative coordinate addition y axis -> (-20 , 20-40) -> (-20,-20)
    5 -> right -> positive coordinate addition x axis -> (-20+50,-20) -> (30,-20)
    6 -> up -> positive coordinate addition to y axid-> (30,-20+60) -> (30, 40)
    7 -> left -> negative coordinate addition to x axis-> (30-70,40) -> (-40,40)
    ************************** my code ***************************************************
    lis=[0,0]
    T=int(input()) #taking input
    counter=10 #increment in distance everytime
    for i in range (1,T+1): #iteration loop to each time adding
    c=i%4 #since we have 4 options right up left and down repeating
    if c==1:
    lis[0]=lis[0]+counter
    if c==2:
    lis[1]=lis[1]+counter
    if c==3:
    lis[0]=lis[0]-counter
    if c==0:
    lis[1]=lis[1]-counter
    counter=counter+10
    print(tuple(lis))

    code is in python


    • Vaibhav Jain

      hey Roopa, this is simple in the code, we have used a switch case, in which every time we are adding 10 more units to the total distance which he has covered, after checking that in which direction he previously was. Thus ‘R’, ‘U’, ‘L’, ‘D’, ‘A’ are the different directions on the basis of which we are adding 10 units to the total distance. I hope you get it, please comment below and let me know if there are still any doubts


    • Aman Varshney

      To know the solutions in brief then you can just join the online courses, where we will discuss each and everything in a very segregated manner.