How to solve the Coding Section in TCS NQT 2020?
What is TCS NQT 2020?
“https://www.conduiraonline.com/index.php/detail/2278-tcs-nqt-2020-registration-exam-date-exam-pattern-eligibility”-TCS NQT 2020 is the exam conducted by TCS for recruiting freshers who are going to graduate in the year 2020. Make sure that you understand the Eligibility criteria for the test, the syllabus and test pattern.
What is the Coding section in NQT 2020?
The coding section of the TCS NQT 2020 has one question which is usually in the form a Case study or a Story. At the end of the Caselet, they will ask us to write a program which takes the input in a particular format and produces the output as per the required format.
What are the other rules for the Coding section?
You can attempt the coding task in any of the 5 languages given by TCS. These are C, C++, Java, Python and Perl. You have a total of 30 minutes to solve this question. Here are the most important points to know about before you attempt the coding section of TCS NQT 2020. To know more check our blog on “https://www.conduiraonline.com/index.php/detail/2288-programming-language-to-answer-tcs-nqt-2020-coding-questions”.
How is the coding task evaluated?
The TCS NQT 2020 is attempted by lakhs of students. The examiners are not going to read everyone’s code. Instead, they will use a computerized evaluation which will automatically assign score based ONLY ON THE OUTPUT.
The main part of the coding section is “Test Cases”. Your code will be Validated against test cases. You will be given partial marks based on how many test cases are cleared.
STEP by STEP guide to solving the coding section in TCS NQT 2020.
STEP 1: Understanding the story.
You are given 30 minutes to write a program and clear all the test cases. Of this, spend the first five to six minutes on understanding the story given and figuring out what needs to be calculated.
If your English is not very strong, you need to spend more time in reading and understanding the task. Don’t jump into typing your program before you understand what is going on.
These are the points that you need to understand from the task.
There are coins of several denominations.
Initially, there are an Even number of coins of each type.
One of the coins is lost. When you remove 1 from an even number, we get an Odd number. So, finally we have many denominations occurring an Even number of times but one particular denomination occurring an Odd number of times.
The first input we get is the total number of coins. Let’s call this N.
In the second line we get only N-1 values because one of the coins is missing.
The output is the denomination (value) of the missing coin.
STEP 2: Reading the inputs
Once you know what is needed, we need to think in terms of how you write the code. At this time we don’t know how to find the answer. But we know how to take the inputs. So, the first part of the program is to read the value of N, declare an array and read N-1 values into the array.
Even though the task keeps changing, reading a set of values in a loop is required in many programs. You will need to practice several programs so that you won’t waste your time in simple tasks like this in the actual exam.
STEP 3: Cracking the core logic.
The next step is to figure out how to convert this data into an answer. By the time we reach this step in the exam, we should have about 20 minutes left.
Let’s race against the time.
Consider the example given in the question. We see that the denomination Rs.2 appears two times, Rs.1 appears two times and Rs.5 appears three times. Why is it so?
We know that originally all these are present an even number of times. But, one of the coins got lost and that is the number which appears an odd number of times. In this example, we can infer that originally, there were four coins with Rs. 5 but one of them fell down. That is why we finally have three Rs.5 coins instead of four Rs.5 coins.
Here is a direct way of solving this problem. Towards the end of this article, we will see a more efficient way of solving the same problem.
Task 1: Read one coin at a time in the loop. Take its value. Let’s call this a[j].
Task 2: In an inner loop, go through each coin in the list and count how many times is V occurring. For this we first need to initialize count to zero. Whenever a[j] == a[i] is true, we need to increment the counter.
Task 3: Once the inner loop is completed, the value of count will tell us how many times has a[i] occurred in the array.
Task 4: We need to check if the count is Odd. We are told that only 1 denomination will occur an odd number of times. If we find it, we can print it and exit the program.
When we divide an Odd number with 2, we get 1 as remainder. This can be obtained using the % operator.
Step 4: Validating the code
The remaining time in the exam can be spent in verifying that the code clears all the test cases. In case it fails, you can try giving your own inputs to find out when it is failing and then try to correct the algorithm.
In TCS NQT 2020, you might not have a big penalty if your code is slow. So, it makes a lot of sense for you to write working code before you worry about efficiency.
Once we realize that we need to find the number which is occurring an Odd number of times, some of us can come up with an alternative method to identify it. This method is based on the EXOR operation which is a bit-wise operation performed using the ^ symbol.
Here is the Truth table for XOR operations.
0 ^ 0 0
0 ^ 1 1
1 ^ 0 1
1 ^ 1 0
From the above Truth table, we can conclude that N ^ N = 0, 0 ^ N = N. Let’s say we have 3 integers A, B and C. Here are a few interesting results from EXOR.
A^B^A = A^A^B = B
A^B^C^B^A^C = 0
So, if we perform the EXOR operation on a series of numbers we will notice some interesting results.
If a particular number (say A) is occurring an Even number of times, the EXOR of all of them together is 0. i.e. A^A^A…^A =0 when the number is occurring an Even number of times.
If a number is occurring an Odd number of times, the EXOR of all the occurrences is same as the number itself. So, A^A^A^….^A= A when the number is occurring an Odd number of times.
The order in which we apply the EXOR operation doesn’t matter A^B=B^A.
Using these properties together, we can notice that when we take the EXOR of all the inputs, any number that occurs an even number of times will give an EXOR of 0. If A is the number that occurs an Odd number of times, the overall EXOR for all its appearances will be equal to A. The overall result for all the other numbers will be zero. So, finally we get A^0 which is equal to A.
Once we know this, we can go ahead and implement this in the code.
We just need to take a temporary variable to store the result. Let’s call this E and initialize it to 0. We then need to go through a loop and perform EXOR on all the given elements. The final value of EXOR is the answer we need.
Do I need to make my program so efficient?
While it is always good to write efficient programs you need to control your greed. Before you worry about efficiency, you need to ensure that your program clears at least some of the test cases. Finally, nobody reads your code – they just look at the number of test cases. Make sure that your code clears as many test cases as possible.
However, if you know an efficient method, there is no reason for you to not use it. Go ahead, give it best shot. This is your playground.
In which language should I write the code?
The advantage of choosing C, C++ and Java over scripting languages is that the compiler is very strict. The probability of any mistakes being found by the compiler is very high which means the probability of validating against the test cases is also high.
On the other hand, in scripting languages like Python or Perl the probability of finding mistakes by compiler is not fruitful. Some of your coding mistakes might percolate till the time you go into the compiler stage.
As we keep telling, don’t try to learn a new programming language now. Stick with a language that you already know. Build your confidence by practicing multiple tasks using it.
Is it necessary to Validate Against Test Cases?
TCS NQT 2020 has introduced the facility of validating your solution code against the actual test cases. Make sure that your code is clearing them. Keep tweaking it until you are done. However, in the last 2 minutes you need to ensure that your code is stable. Stop making any further changes. Read your code a few times to ensure that you haven’t done anything stupid in your excitement.
Where can I get more questions like this?
You can find more on “https://www.conduiraonline.com/index.php/detail/2297-tcs-nqt-coding-questions-and-how-coding-question-evaluated-in-tcs-nqt”and mock tests include questions in the NQT pattern.
- SAP Placement Coding Questions
- Samsung RnD Coding Round Questions
- BrowserStack Interview Experience | Set 2 (Coding Questions)
- BrowserStack Interview Experience | Set 3 (Coding Questions)
- BrowserStack Interview Experience | Set 7 (Online Coding Questions)
- Oyo Coding Interview Experience
- Coding Mart Interview Experience
- Informatica Coding Round Question
- Microsoft Non-tech Coding round
- How to answer a coding question in an Interview?
- TCS Coding Practice Question | Check Odd or Even
- TCS Coding Practice Question | HCF or GCD of 2 Numbers
- TCS Coding Practice Question | LCM of 2 Numbers
- TCS National Qualifier 2 Coding Question.
- Thoughtworks Interview Experience | coding round
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.