TCS NQT Interview Experience (Off-Campus) 2024

In recent years, the TCS National Qualifier Test (NQT) has become a gateway for many aspiring candidates to kickstart their careers in the tech industry. However, the coding section of the exam has been a subject of debate among candidates due to its ambiguous and sometimes inadequately described questions. In this article, we’ll dissect one such coding question from the TCS NQT conducted on April 26th, 2024, shedding light on the issues with its description and providing a detailed example.

The Problem:

One of the questions posed in the coding section of the TCS NQT on April 26th, 2024, revolved around finding all subarrays of a given array whose sum of elements is equal to a specified value, let’s call it K.

The Issue:

The problem statement lacked clarity, especially concerning how input should be handled when the size of the array is not provided upfront. Moreover, it failed to specify how input containing non-integer characters should be managed, leading to confusion among candidates.

An Example:

Consider the following input scenario:

Input: 1 2 3 4 5 -4 -3 ,10

In this example, the input consists of integers separated by spaces, with a comma separating the array from the specified value of K, which is 10. However, the problem statement does not clarify how to handle non-integer characters like commas in the input.

The Solution:

To address the ambiguity in the problem statement, let’s provide a solution along with the necessary input handling. Below is the code snippet demonstrating how to handle input until a non-integer character is encountered:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> arr;
    int num;
    char ch;

    // Input loop
    while (true) {
        // Read an integer
        if (cin >> num) {
            arr.push_back(num);
        } else {
            // If reading an integer fails, clear the error flag and read the next character
            cin.clear();
            cin >> ch;
            // If the character is not a comma, break the loop
            if (ch != ',') {
                break;
            }
        }
    }

    // Read the value of K
    int sum;
    cin >> sum;

    // Logic to find subarrays with sum equal to K
    // (Implementation omitted for brevity)
    
    return 0;
}
Java
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        ArrayList<Integer> arr = new ArrayList<>();

        // Input loop
        while (true) {
            try {
                // Read an integer
                int num = scanner.nextInt();
                arr.add(num);
            }
            catch (Exception e) {
                // If reading an integer fails, clear the
                // input buffer
                scanner.nextLine();
                break;
            }
        }

        // Read the value of K
        int sum = scanner.nextInt();

        // Logic to find subarrays with sum equal to K
        // (Implementation omitted for brevity)
    }
}

Conclusion:

The TCS NQT coding questions often present challenges due to their vague descriptions and ambiguous requirements. As demonstrated in this article, candidates need to be equipped with not only coding skills but also the ability to interpret and clarify unclear problem statements. It’s crucial for examination authorities to provide well-defined questions to ensure a fair and accurate assessment of candidates’ abilities.

Exact Solution For the Above Question :

C++
#include <bits/stdc++.h>
using namespace std;

int main()
{
    // input--  //1 2 3,852
    vector<int> arr;
    int num;
    char ch;

    while (true) {
        if (cin >> num) {
            arr.push_back(num);
        }
        else {
            cin.clear();
            cin >> ch;
            break;
        }
    }

    int sum;
    cin >> sum;

    vector<vector<int> > ans;
    int n = arr.size();

    for (int i = 0; i < n; i++) {
        int curr = 0;
        vector<int> temp;
        for (int j = i; j < n; j++) {
            curr += arr[j];
            temp.push_back(arr[j]);
            if (curr == sum) {
                ans.push_back(temp);
            }
        }
    }

    for (int i = 0; i < ans.size(); i++) {
        for (int j = 0; j < ans[i].size(); j++) {
            cout << " " << ans[i][j];
        }
        cout << endl;
    }

    return 0;
}