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 ….”


  • Gyanendra

    import java.util.*;
    public class Main
    {
    public static void main(String[] args) {
    Scanner scn=new Scanner(System.in);
    int n=scn.nextInt();
    getdistance(n);
    }

    public static void getdistance(int n){
    int x=0,y=0;
    char ch=’R’;
    int distance=10;

    while(n>0){
    switch(ch){
    case ‘R’:
    x+=distance;
    distance+=10;
    ch=’U’;
    break;

    case ‘U’:
    y+=distance;
    distance+=10;
    ch=’L’;
    break;

    case ‘L’:
    x-=distance;
    distance+=10;
    ch=’D’;
    break;

    case ‘D’:
    y-=distance;
    distance+=10;
    ch=’A’;
    break;

    case ‘A’:
    x+=distance;
    distance+=10;
    ch=’R’;
    break;
    }
    n–;
    }

    System.out.println(x+” “+y);
    }
    }


  • Riya

    Testcase 6 will be 90 -20 and 7 will be 90 50
    n = int(input())
    x,y=0,0
    for i in range(1,n+1):
    c=i%5
    if c == 1:
    x = x+(10*i)
    elif c == 2:
    y = y+(10*i)
    elif c == 3:
    x = x-(10*i)
    elif c == 4:
    y = y-(10*i)
    else:
    x = x+(10*i)

    print(x,y)