Question: TCS Xplore Online Assessment (OA) Coding Questions & Solutions | Feb 2025 Placement
0
Entering edit mode

Question 1: Zlatan and Tricks

Problem Statement:

Zlatan is a master of solving tricky questions. His friend gave him some numbers (in the form of an array) and asked him to find the number of subsequences of the array such that its sum is m-1.

Here, m is the sum of the initial given array. Output the number of such subsequences and print it in one line.

Example:

array = [5, 4, 2, 1, 1]

Output: 2

Valid ways are taking the first [5, 4, 2, 1] and then the second [5, 4, 2, 1].

Question 2: Professor Plays a Game

Problem Statement: A professor wanted maximum benefits from a number game. He has information on numbers scattered throughout a game board given in the form of an array. He will surely take three numbers and multiply their values to get the maximum value out of all possible combinations. The array might contain negative numbers as well.

Simply print the maximum value he can get.

Example: array = [-9, 9, 5, -3, 100], n = 5

Output: 4500 Explanation: The three numbers chosen are 9, 5, & 100 which have a multiplication value of 4500.
 

Question 3: Forgotten Number

Problem Statement:

You are given a number n. Your friend added the numbers from 1 till n, TWICE. The sum is S. Now he says that he forgot to add only one number twice (he added it only once).

Determine the number he may have possibly missed out.

Example:

n = 4, S = 14

Output: 3

Explanation: If we add numbers twice from 1 till 4, we get 20. He has a sum of 14, which means 20 - 14 = 6 is missing. But wait, he missed adding a number twice, meaning it's only in the sum once. Expected S = 20, actual S = 14. But the example logic says 20 - 3 - 3 = 14. Wait, the math is expected total minus actual total equals the missing number.
 


Question 4: King

Problem Statement: A king is a person who has:

  1. More kindness than his neighbours.

  2. More kindness than the rest of the people.

Kindness is given by an integer value in an array. Find the position of the king. If there are multiple kings, print all of their positions separated by spaces. (1-based indexing)

Example: kind = [5, 6, 7, 6, 5, 5, 100, 5, 100, 45], n = 11

Output: 8 10 Notice there are two kings, one at the 8th and the other at the 10th position.
 

Question 5: Class Test 1

Problem Statement: In a class test, many students participated. You have to determine both the number of unique scores and the unique scores as scored by the students.

In the first line, print the unique scores in the order they appear. In the second line, print: "The number of unique scores are X", where X is the number of unique scores.

Example: array = [45, 61, 78, 78, 98, 98]

Output: 45 61 78 98 The number of unique scores are 4
 

Question 6: Sub-Array

Problem Statement:

Given an array of integers of length n. Find out if there exists a contiguous subarray whose sum is equal to the number K.

If there exists a subarray with a sum equal to K, print the largest and the smallest number in that subarray. If there exists no such subarray, print "-1 -1".

Example:

array = [1, 5, 2, 6, 3, 2], n = 6, k = 7

Output: 5 2

Explanation: The subarray [5, 2] has a sum of 7. The largest value is 5 and the smallest is 2..

ADD COMMENTlink 8 days ago admin 1.9k
Entering edit mode
0

Problem-1 Zlatan and Tricks Solution

Topics - : 

  • Array Mathematics

  • Combinatorics / Counting

Overview

The total sum of the entire array is exactly m. To form a subsequence with a sum of m - 1, the elements we leave out must sum to exactly 1. Therefore, the number of such subsequences is simply the total count of the number 1 present in the initial array.

Approach

1. The Logic Behind the Sum

This is a clever logic puzzle! Instead of building every possible combination to see what works, we just look at what we are leaving behind. If the total array sums up to m, and we want a piece of it that sums to m - 1, the piece we remove must be exactly 1.

2. Counting the Targets

Since we just need to drop a single 1 to get our desired sum, every individual 1 in the array represents a valid, unique way to form the subsequence. We just iterate through the array and count how many times the number 1 appears.

Code Implementation

function countSubsequences(n, array):
    countOfOnes = 0
    
    for i from 0 to n - 1:
        if array[i] == 1:
            countOfOnes = countOfOnes + 1
            
    return countOfOnes

 

ADD REPLYlink 8 days ago
admin
1.9k
Entering edit mode
0

Problem-2 Professor Plays a Game Solution

Topics Involved / Prerequisites

  • Array Sorting

  • Greedy Approach

Overview

  1. Sorting the array allows us to easily access the largest positive and smallest negative numbers.
  2. The maximum product of three numbers usually comes from the three largest positive numbers at the end.
  3. However, multiplying the two smallest negative numbers yields a large positive number, which must be checked against the largest element.

Approach

1. Sorting the Board This is a classic optimization challenge! When dealing with maximum products, we always want the absolute highest values. By sorting the array in ascending order, we push the largest positive numbers to the far right, and the largest negative numbers (smallest values) to the far left.

2. Comparing the Two Extremes There are only two ways to get the maximum product of three numbers:

  • Multiply the three largest numbers together: array[n-1] * array[n-2] * array[n-3]

  • Multiply the two smallest negative numbers (which makes a positive) by the absolute largest number: array[0] * array[1] * array[n-1] We calculate both and simply return whichever is bigger!

Code Implementation

function maxProductOfThree(n, array):
    sort(array)
    

    opt1 = array[n-1] * array[n-2] * array[n-3]
    opt2 = array[0] * array[1] * array[n-1]
    
    return max(opt1, opt2)

 

ADD REPLYlink 8 days ago
admin
1.9k
Entering edit mode
0

Forgotten Number Solution

Topics -:

  • Mathematical

Overview

The expected sum if every number from 1 to n is added exactly twice is simply n *(n + 1). By subtracting the actual given sum S from this perfect expected total, we get the total value that is missing. Since the logic implies the missing number was omitted completely (missed twice), we divide this difference by 2.

Approach

1. Finding the Perfect Sum

This is a fun mathematical trick! Rather than simulating the friend's exact additions, we use the classic sum formula. The sum of 1 to n is n* (n + 1) / 2. Since he was supposed to add them twice, the expected sum is just n*(n + 1).

2. Finding the Difference

We subtract the friend's actual sum S from our perfect expected sum. The problem's example output shows that the missing number was skipped both times. So, the difference we calculated is actually twice the value of the forgotten number. We just divide the difference by 2 to get our final answer!

function findForgottenNumber(n, S):

    expectedSum = n * (n + 1)

    difference = expectedSum - S
    
    return difference / 2

Time Complexity

  • Time: O(1) - The entire calculation is done using a direct mathematical formula.

  • Space: O(1) - No extra memory is used.

ADD REPLYlink 8 days ago
admin
1.9k
Entering edit mode
0

Problem-4 King Solution

Topics-: Array

Overview

A king must have more kindness than everyone else, meaning we are looking for the absolute maximum values in the entire array. We first scan the array to determine this highest possible kindness score among all people. Then, we scan again to find the positions of this maximum score, ensuring they are strictly greater than their immediate left and right neighbors.

Approach

Approach

1. Finding the Highest Standard This requires a two-step validation! To be a true king, you must first have more kindness than the general population. This means the king can only be someone who shares the maximum value found in the entire array.

2. Checking the Neighbors Once we know what the maximum value is, we loop through the array again. If we find someone with that maximum value, we check the person to their immediate left and right. If they are strictly greater than both neighbors (handling the edges carefully), we print their 1-based index.

Psudo Code 

function findKings(n, kind):
    maxKindness = -1
    
    // Step 1: Find the absolute maximum
    for i from 0 to n - 1:
        maxKindness = max(maxKindness, kind[i])
        
    // Step 2: Validate the neighbors
    for i from 0 to n - 1:
        if kind[i] == maxKindness:
            greaterThanLeft = (i == 0) OR (kind[i] > kind[i-1])
            greaterThanRight = (i == n - 1) OR (kind[i] > kind[i+1])
            
            if greaterThanLeft AND greaterThanRight:
                print(i + 1 + " ") 

 

ADD REPLYlink 8 days ago
admin
1.9k
Entering edit mode
0

Problem5 Class Test 1 Solution

Topics 

  • Hash Sets

Overview

  1. To find unique scores while maintaining their original order, we need to filter out any duplicates as we read the list.
  2. We can use a hash set to keep track of which scores we have already encountered and printed to the screen.
  3. Finally, the total size of this hash set will give us the exact number of unique scores to print at the end.

Approach

1. Filtering in Real-Time This is a straightforward data filtering task! The key is to maintain the original sequence. As we loop through the test scores, we check if the current score is already in our Hash Set.

2. Storing and Printing If the score is not in the set, it's a brand new unique score! We print it out immediately, and then add it to our set so we don't print it again later. Once the loop finishes, the length of our set is the total number of unique scores.

Code Implementation-: 

function processScores(n, array):
    create an empty Hash Set 'seen'
    
    // Print unique elements in order
    for i from 0 to n - 1:
        if array[i] is NOT in seen:
            print(array[i] + " ")
            seen.insert(array[i])
            
    print new line
    
    // Print total count
    print("The number of unique scores are " + seen.size())

Time Complexity

  • Time: O(N) - We iterate through the array once to find the subarray, and at most once more to find the min/max inside it.

  • Space: O(N) - In the worst case, we store N distinct prefix sums in the hash map.

ADD REPLYlink 8 days ago
admin
1.9k

Login before adding your answer.

Similar Posts
Loading Similar Posts