TCS NQT – October 3rd, 2024 (Shift 1)


Question 1: Weekly Exercise Summary

Problem Statement:
You have to design a weekly exercise summary by taking the number of minutes of daily exercise for 7 consecutive days.
The exercise duration for all days will be entered by the user.

Your task is to:

  1. Calculate the total exercise duration for the week
  2. Calculate the average daily workout duration (rounded to nearest integer)

Input Format:

Day 1 exercise duration: <minutes> Day 2 exercise duration: <minutes> ... Day 7 exercise duration: <minutes> 

Output Format:

Exercise summary Total exercise duration : <total> (minutes) Average daily exercise duration: <average> minutes 

Example Input:

Day 1 exercise duration: 45 Day 2 exercise duration: 15 Day 3 exercise duration: 30 Day 4 exercise duration: 15 Day 5 exercise duration: 5 Day 6 exercise duration: 10 Day 7 exercise duration: 20 

Example Output:

Exercise summary Total exercise duration : 140 (minutes) Average daily exercise duration: 20 minutes 


Solution (Java):

import java.util.Scanner;

public class ExerciseSummary {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] exerciseDuration = new int[7];
        int total = 0;
        
        for (int i = 0; i < 7; i++) {
            System.out.print(“Day ” + (i + 1) + ” exercise duration: “);
            exerciseDuration[i] = scanner.nextInt();
            total += exerciseDuration[i];
        }
        
        double average = total / 7.0;
        average = Math.round(average * 100.0) / 100.0;
        
        System.out.println(“Exercise Summary”);
        System.out.println(“Total exercise duration: ” + total + ” minutes”);
        System.out.println(“Average daily exercise duration: ” + average + ” minutes”);
        
        scanner.close();
    }
}

Question 2: Problem Statement:
You are given a range of integers from M to N (inclusive), where:

  • M is the lower limit
  • N is the upper limit

Your task is to find all palindrome numbers in the range and return the count of such numbers.

Input Format:

Enter M : <lower limit integer> Enter N : <upper limit integer> 

Output Format:

<number of palindrome numbers> 

Definition:
palindrome number is a number that reads the same backward as forward.
Example: 121, 131, 11, 9

Example Input:

Enter M : 10 Enter N : 20 

Example Output:

Explanation:
Only one palindrome exists between 10 and 20 → 11


Solution (C++):


#include <iostream>
using namespace std;

bool isPalindrome(int number) {
    if (number < 0) return false;
    int original = number, reversed = 0;
    while (number > 0) {
        reversed = reversed * 10 + number % 10;
        number /= 10;
    }
    return original == reversed;
}

int main() {
    int M, N;
    cout << “Enter M: “;
    cin >> M;
    cout << “Enter N: “;
    cin >> N;
    int count = 0;
    for (int i = M; i <= N; i++) {
        if (isPalindrome(i)) count++;
    }
    cout << count << endl;
    return 0;
}

TCS NQT – October 3rd, 2024 (Shift 2)

 

Question 3: Train Travel Time
Problem: A train covers 800 meters (400m track + 400m bridge) at a given speed (km/hr). Calculate the time taken in seconds using the formula: (Total distance / Speed) * (18/5).


Solution (C++):


#include <iostream>
using namespace std;

int main() {
    double speed;
    cin >> speed;
    int totalDistance = 800;
    double speedMs = speed * 5.0 / 18.0; // Convert km/hr to m/s
    int time = totalDistance / speedMs;
    cout << time << endl;
    return 0;
}

Question 4: Split Array with Equal Averages
Problem: Given an array of integers, check if it can be split into two subarrays with equal averages. Output true or false.


Solution (Java):
import java.util.Scanner;

public class ArraySplit {
    public static boolean canSplit(int[] arr, int n) {
        int totalSum = 0;
        for (int i = 0; i < n; i++) totalSum += arr[i];
        int leftSum = 0;
        for (int i = 0; i < n – 1; i++) {
            leftSum += arr[i];
            int rightSum = totalSum – leftSum;
            if (leftSum * (n – i – 1) == rightSum * (i + 1)) return true;
        }
        return false;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) arr[i] = sc.nextInt();
        System.out.println(canSplit(arr, n) ? “true” : “false”);
        sc.close();
    }
}

https://youtube.com/watch?v=3YfX4gTBJzc%3Fsi%3DLCUGvXUGZKBI7Jmn

TCS NQT – October 4th, 2024 (Shift 1)


Question 5: Perfect Donation Amount
Problem: Check if a donation amount is a perfect number (sum of its proper divisors equals the number). Return true if perfect, false otherwise.


Solution (Java):


import java.util.Scanner;

public class PerfectNumber {
    public static boolean isPerfect(int num) {
        int sum = 0;
        for (int i = 1; i <= Math.sqrt(num); i++) {
            if (num % i == 0) {
                sum += i;
                if (num / i != i && num / i != num) sum += num / i;
            }
        }
        return sum == num;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        System.out.println(isPerfect(num));
        sc.close();
    }
}

Question 6: Inventory Frequency Counter
Problem: Given a string of space-separated item names, count the frequency of each item. If the input contains digits, output “Invalid input.”


Solution (Java):


import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class InventoryManager {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        Map<String, Integer> freq = new HashMap<>();
        String word = “”;
        
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) >= ‘0’ && s.charAt(i) <= ‘9’) {
                System.out.println(“Invalid input”);
                return;
            } else if (s.charAt(i) == ‘ ‘) {
                if (!word.isEmpty()) freq.put(word, freq.getOrDefault(word, 0) + 1);
                word = “”;
            } else {
                word += s.charAt(i);
            }
        }
        if (!word.isEmpty()) freq.put(word, freq.getOrDefault(word, 0) + 1);
        
        for (Map.Entry<String, Integer> entry : freq.entrySet()) {
            System.out.println(entry.getKey() + ” ” + entry.getValue());
        }
        sc.close();
    }
}

TCS NQT – October 4th, 2024 (Shift 2)


Question 7: Calculate Speed in km/hr
Problem: Given a distance of 1000 meters and time in seconds, calculate the speed in km/hr.


Solution (C++):


#include <iostream>
using namespace std;

int main() {
    double time;
    cin >> time;
    double distance = 1000.0; // meters
    double speedMs = distance / time; // m/s
    double speedKmh = speedMs * 18.0 / 5.0; // Convert m/s to km/hr
    cout << speedKmh << endl;
    return 0;
}

Question 8: Jump Game
Problem: Given an array where each element is the maximum jump length from that index, check if you can reach the last index from the first index.


Solution (Java):

import java.util.Scanner;

public class JumpGame {
    public static boolean canJump(int[] nums) {
        int maxReach = 0;
        for (int i = 0; i < nums.length && i <= maxReach; i++) {
            maxReach = Math.max(maxReach, i + nums[i]);
            if (maxReach >= nums.length – 1) return true;
        }
        return false;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        String[] s = input.split(“,”);
        int[] nums = new int[s.length];
        for (int i = 0; i < s.length; i++) {
            nums[i] = Integer.parseInt(s[i].trim());
        }
        System.out.println(canJump(nums));
        sc.close();
    }
}

Tips for TCS NQT Success

  • Practice Input Handling: Many questions use Scanner (Java) or cin (C++). Test with sample inputs to avoid errors.
  • Master Arrays and Strings: Most problems involve arrays or string manipulation. 
  • Understand Math Basics: Questions like Train Travel Time and Speed Calculation require unit conversions. Brush up on formulas!
  • Learn HashMaps: The Inventory Frequency Counter uses a HashMap, which is super common in coding tests.
  • Test Edge Cases: For example, in the Jump Game, check if an array with one element or zeros works correctly.
  • Time Management: TCS NQT has tight time limits. Solve easier problems first, then tackle complex ones like Jump Game.