Menu

Navigation

HomeAll CompaniesPrevious Year QuestionsTNP / PlacementsBEU ResultDocumentation

Companies

InfosysTCSWiproAccentureCognizantCapgemini
R's PlacePrep|
CompaniesPYQDocs

Company

TCS

Content

2025 NQT2024 NQTAptitudeCoding

View Options

Display Mode

Sections

Practice Questions7

Export

PDF

Word

Last updated: Feb 11, 2026

TCS

Coding

Display Mode

Jump to Section

Practice Questions7

Other Content

2025 NQT2024 NQTAptitudeCoding

Export

PDF

Word

Home/Companies/TCS/coding
Back to TCS

TCS coding

1 sections, 7 questions

Practice Questions

7 questions
Question 1

Output Prediction

Problem

What will be the output of the following C code?

#include <stdio.h>

int main() {
    int x = 5;
    printf("%d", x++ + ++x);
    return 0;
}

Solution

x++ is post-increment: uses value 5, then increments to 6
++x is pre-increment: increments 6 to 7, then uses value 7

Result: 5 + 7 = 12

Question 2

Loop Analysis

Problem

How many times will the loop execute?

int i = 0;
while (i < 5) {
    printf("%d ", i);
    i++;
}

Solution

Loop executes for i = 0, 1, 2, 3, 4 (5 times)

Question 3

Array Manipulation

Problem

Find the second largest element in an array.

Example:

Input: [12, 35, 1, 10, 34, 1]
Output: 34

Solution

Iterate through array maintaining first and second largest elements.

Code

Java
public int findSecondLargest(int[] arr) {
    if (arr.length < 2) return -1;
    
    int largest = Integer.MIN_VALUE;
    int secondLargest = Integer.MIN_VALUE;
    
    for (int num : arr) {
        if (num > largest) {
            secondLargest = largest;
            largest = num;
        } else if (num > secondLargest && num != largest) {
            secondLargest = num;
        }
    }
    
    return secondLargest == Integer.MIN_VALUE ? -1 : secondLargest;
}
C
#include <limits.h>

int findSecondLargest(int arr[], int n) {
    if (n < 2) return -1;
    
    int largest = INT_MIN;
    int secondLargest = INT_MIN;
    
    for (int i = 0; i < n; i++) {
        if (arr[i] > largest) {
            secondLargest = largest;
            largest = arr[i];
        } else if (arr[i] > secondLargest && arr[i] != largest) {
            secondLargest = arr[i];
        }
    }
    
    return secondLargest == INT_MIN ? -1 : secondLargest;
}
Python
def find_second_largest(arr):
    if len(arr) < 2:
        return -1
    
    largest = second_largest = float('-inf')
    
    for num in arr:
        if num > largest:
            second_largest = largest
            largest = num
        elif num > second_largest and num != largest:
            second_largest = num
    
    return -1 if second_largest == float('-inf') else second_largest
Time: O(n)Space: O(1)
Question 4

String Processing

Problem

Reverse words in a string.

Example:

Input: "the sky is blue"
Output: "blue is sky the"

Solution

Split string into words, reverse the order, and join.

Code

Java
public String reverseWords(String s) {
    String[] words = s.trim().split("\\s+");
    StringBuilder result = new StringBuilder();
    
    for (int i = words.length - 1; i >= 0; i--) {
        result.append(words[i]);
        if (i > 0) result.append(" ");
    }
    
    return result.toString();
}
Python
def reverse_words(s):
    words = s.strip().split()
    return ' '.join(reversed(words))
Time: O(n)Space: O(n)
Question 5

Array Rotation

Problem

Rotate an array to the right by k positions.

Example:

Input: nums = [1,2,3,4,5,6,7], k = 3
Output: [5,6,7,1,2,3,4]

Solution

Use reversal algorithm for in-place rotation.

Code

Java
public void rotate(int[] nums, int k) {
    int n = nums.length;
    k = k % n;
    reverse(nums, 0, n - 1);
    reverse(nums, 0, k - 1);
    reverse(nums, k, n - 1);
}

private void reverse(int[] nums, int start, int end) {
    while (start < end) {
        int temp = nums[start];
        nums[start] = nums[end];
        nums[end] = temp;
        start++;
        end--;
    }
}
Python
def rotate(nums, k):
    k = k % len(nums)
    nums.reverse()
    nums[:k] = reversed(nums[:k])
    nums[k:] = reversed(nums[k:])
Time: O(n)Space: O(1)
Question 6

Palindrome Check

Problem

Check if a number is palindrome.

Example:

Input: 121
Output: true

Solution

Reverse the number and compare with original.

Code

Java
public boolean isPalindrome(int x) {
    if (x < 0) return false;
    int original = x;
    int reversed = 0;
    
    while (x > 0) {
        reversed = reversed * 10 + x % 10;
        x /= 10;
    }
    
    return original == reversed;
}
C
int isPalindrome(int x) {
    if (x < 0) return 0;
    int original = x;
    int reversed = 0;
    
    while (x > 0) {
        reversed = reversed * 10 + x % 10;
        x /= 10;
    }
    
    return original == reversed;
}
Python
def is_palindrome(x):
    if x < 0:
        return False
    original = x
    reversed_num = 0
    
    while x > 0:
        reversed_num = reversed_num * 10 + x % 10
        x //= 10
    
    return original == reversed_num
Time: O(log n)Space: O(1)
Question 7

Factorial

Problem

Calculate factorial of a number.

Example:

Input: 5
Output: 120

Solution

Multiply all numbers from 1 to n.

Code

Java
public long factorial(int n) {
    if (n <= 1) return 1;
    long result = 1;
    
    for (int i = 2; i <= n; i++) {
        result *= i;
    }
    
    return result;
}
C
long factorial(int n) {
    if (n <= 1) return 1;
    long result = 1;
    
    for (int i = 2; i <= n; i++) {
        result *= i;
    }
    
    return result;
}
Python
def factorial(n):
    if n <= 1:
        return 1
    result = 1
    
    for i in range(2, n + 1):
        result *= i
    
    return result
Time: O(n)Space: O(1)

Last updated: 2026-02-11

Radhika's PlacePrep built by Rohan Sharma