Table of contents
1.
Introduction
2.
Problem Description
2.1.
Sample Examples
3.
Approach
3.1.
Algorithm
3.2.
Implementation in C++
3.3.
Implementation in Python
3.4.
Implementation in Java
4.
Frequently Asked Questions
4.1.
What is TCS CodeVita?
4.2.
What is the eligibility to participate in TCS CodeVita?
4.3.
What are the languages we can code in the exam?
5.
Conclusion
Last Updated: Aug 13, 2025
Hard

TCS CodeVita Questions

Author Akshay Parashar
3 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

TCS organizes CodeVita every year, in which any graduate student can participate and give their all. It is used to encourage competitive programming and to help you improve your programming skills. In addition, top scorers are allowed to interview for job openings with TCS. The first CodeVita was held in India in 2012 to raise awareness about competitive coding and various applications and codings.

In this blog, we will solve a sample question from TCS CodeVita but before that, if you are looking for a complete guide on how to Crack TCS NQT & Hack With Infy and Get Placement Offers, refer to the following video.

(Also see Data Structures)

Problem Description

A 10cm x 10cm x 10cm solid cube rests on the ground. It has a beetle on it, as well as some sweet honey spots on the cube's surface. The beetle begins at a point on the cube's surface and moves in a clockwise direction along the cube's surface to the honey spots.

1. If it goes from one point on the same face to another (say, X to Y), it goes in an arc of a circle that subtends an angle of 60 degrees at the circle's center.

2. If it travels from one point to another on a different face, it takes the shortest path on the cube's surface, except that it never travels along its bottom.

The beetle is a Cartesian geometry student who knows the coordinates (x, y, z) of all the points it needs to visit. Its coordinate origin is one of the cube's corners on the ground, and the z-axis points up. As a result, z=0 is the bottom surface (on which it does not crawl), and z=10 is the top. The beetle keeps track of all distances traveled and rounded the distance to two decimal places when it arrives at the following location so that the final distance is a sum of the rounded distances from spot to spot.

Input

The first line returns an integer N, the total number of points visited by the beetle (including the starting point).

The second line contains 3N non-negative numbers, each with two decimal places. These are to be interpreted as the x, y, and z coordinates of the points the beetle must visit in the given order.

Output

One line contains a number representing the total distance traveled by the beetle to two decimal places. Even if the travel distance is an integer, the output should have two decimal places.

Constraints

None of the points visited by the beetle are on the bottom face (z=0) or on any of the cube's edges (the lines where two faces meet)

2<=N<=10

Difficulty Level

Complex

Time Limit (secs)

1

Sample Examples

Example 1

Input

3
1 1 10 2 1 10 0 1 9

Output

4.05

Explanation

The beetle visits three different locations (N=3). The beetle begins on the cube's top face (z=10) at point (1,1,10) and moves to another point on the same face (2,1,10). Although the straight line distance is one, it travels on the arc of a circle with an angle of 60 degrees at its center and thus travels (2*pi)/6 or 1.05 (note that it rounds the distance at each leg of the journey). It moves along the cube's surface from (2,1,10) on the face z=10 to (0,1,9) on the face x=0. This is a three-mile distance. The total travel distance is 1.05+3=4.05. The result is 4.05

Example 2

Input

3
1 1 10 2 1 10 0 5 9

Output

6.05

Explanation

The beetle visits three different locations (N=3). The beetle begins on the cube's top face (z=10) at point (1,1,10) and moves to another point on the same face (2,1,10). The same as before. This distance is 1.05 miles. It moves along the cube's surface from (2,1,10) on the face z=10 to (0,5,9) on the face x=0. The shortest distance between these points on the cube's surface is 5. 1.05+5=6.05 is the total distance traveled. The result is 6.05.

Approach

To solve this problem, we will first traverse the list and store the points in a list with an increment of 3 from 3 to 3*n-1. We will check if the x or y coordinate is the same as the previous point and z coordinate is the same as the previous point if so the bee will travel in an arc else the bee will always take the shortest route.

Algorithm

  • We are storing the point in a list and traversing the list with an increment of 3 from index 3 to 3*n-1.
  • To calculate distance, if the z-axis coordinate is the same as the previous point and any of the x or y coordinates is the same as the previous point, the bee will travel in an arc.
  • Otherwise, the bee will take the shortest route.

Implementation in C++

#include <bits/stdc++.h>
using namespace std;
#define PIE 3.14
int sx,sy,sz;
float shortDist(int x,int y,int z)
{
    float dist;
    // checking if the Z-axis and any other one axis are the same.
    if(z == sz && ( y == sy || x == sx) && sz != 0) {
    //checking if the x axis of next co-ordinate is same
        if(x != sx){  
        dist = (2 * PIE * (abs(x-sx)))/6.0;
        }
        //checking if the y axis of next co-ordinate is same 
        else
        dist = (2 * PIE * (abs(y-sy)))/6.0;
    }
    //else meaning bee is moving to another face
    else{ 
    // finding eculidean distance between x and y and the abs distance of Z axis
    dist = (int)(sqrt(pow(x-sx,2) + pow(y-sy,2)) + abs(z-sz)); 
    }
    sx = x;sy = y;sz = z;
    return dist;
}
int main()
{
    int n,arr[50],x,y,z;
    float sum = 0.0;
    cin>>n;
    n = 3 * n;
    for(int i = 0;i < n;i++)
    {
        cin>>arr[i];
    }
    sx = arr[0];
    sy = arr[1];
    sz = arr[2];
    for(int i=3;i<n;i+=3)
    {
        sum += shortDist(arr[i],arr[i+1],arr[i+2]);
    }
    printf("%.2f",sum);
} 
You can also try this code with Online C++ Compiler
Run Code

Input

3
1 2 10 2 1 10 1 1 9

Output

3.00

Implementation in Python

import math 
PIE=3.14
def shortDist( x,y,z,sx,sy,sz):
    dist = 0.0
    # if the x or y axis same as Z-axis
    if(z == sz and (y == sy or x==sx) and sz != 0): 
    # if the x axis of next co-ordinate is same 
        if(x != sx):    
            dist = (2 * PIE * (abs(x - sx)))/6.0
    # if the y axis of next co-ordinate is same
        else:  
            dist = (2 * PIE * (abs(y -sy)))/6.0    
    # else means bee is going to another face
    #finding eculidean distance
    else: 
        dist = int((math.sqrt(pow(x-sx,2) + pow(y-sy,2)) + abs(z - sz)))          
    #new starting cordinates
    sx = x
    sy = y
    sz = z  
    return dist,sx,sy,sz
n=int(input())
arr = [int(x) for x in input().split()]
#starting co-ordinate
sx = arr[0]
sy = arr[1]
sz = arr[2]
ans=0
for j in range(3,3*n,3):
    x,sx,sy,sz= shortDist(arr[j],arr[j+1],arr[j+2],sx,sy,sz)
    ans += x
print(round(ans,2))
You can also try this code with Online Python Compiler
Run Code

Input

3
2 2 10 2 1 10 2 1 8

Output

3.05

Implementation in Java

import java.util.*;
import java.lang.*;
import java.io.*;
class Main{
    final static float PI = 3.14f;
    static int sx, sy, sz;
    public static void main(String[] args) {
        int i, N, x, y, z;
        int arr[] = new int[50];
        float sum = 0.0f;
        Scanner sc = new Scanner(System.in);
        N = sc.nextInt();
        N = 3 * N;
        for(i = 0; i < N; i++){
            arr[i] = sc.nextInt();
        }
        sx = arr[0];
        sy = arr[1];
        sz = arr[2];
        for(i = 3; i < N ; i += 3){
            sum += shortDist(arr[i], arr[i+1], arr[i+2]);
        }
        System.out.printf("%.2f", sum);
    }
    private static float shortDist(int x, int y, int z) {
        float dis;
        if(z == sz && (y == sy || x == sx ) && sz != 0){
            if(x != sx){
                dis = (2 * PI * (Math.abs(x - sx))) / 6.0f;
            }else{
                dis = (2 * PI * (Math.abs(y - sy))) / 6.0f;
            }
        }else{
            dis = (int)(Math.sqrt(Math.pow(x - sx, 2) + Math.pow(y - sy, 2)) + Math.abs(z - sz));
        }
        sx = x;
        sy = y;
        sz = z;
        return dis;
    }
}
You can also try this code with Online Java Compiler
Run Code

Input

3
2 2 10 2 1 10 2 1 9

Output

2.05

Frequently Asked Questions

What is TCS CodeVita?

CodeVita is an online coding competition with three significant rounds: the Pre-Qualifier Round, the Qualifier Round, and the Grand Finale. 1. Pre-Qualifier Round: This will be an online coding competition in which participants must answer questions in 6 hours.

What is the eligibility to participate in TCS CodeVita?

Students from India's institutes who will complete their academic programs in 2022, 2023, 2024, and 2025 are eligible for the CodeVita competition.

What are the languages we can code in the exam?

Following are the languages we can use: C, C++, C#, Java, Perl, PHP, Python, and Ruby.

Conclusion

TCS organizes CodeVita every year, in which any graduate student can participate and give their all. It is used to encourage competitive programming and to help you improve your programming skills. In addition, top scorers are allowed to interview for job openings with TCS. In this blog, we have solved a sample question from TCS Codevita. 

Do you know TCS conducts one more placement exam to hire freshers - TCS NQT, refer to the blog TCS NQT Recruitment Process by Coding Ninjas, which has a stepwise explanation of the complete recruitment process.

You can also consider our Competitive Programming Course to give your career an edge over others!

Live masterclass