Mounya mallik Sunkara
535 pages
Mounya mallik Sunkara
535 pages
161
/ 535
TCS SAMPLE CODING QUESTIONS
Problem Statement
A chocolate factory is packing chocolates into the packets. The chocolate packets here
represent an array of N number of integer values. The task is to find the empty
packets(0) of chocolate and push it to the end of the conveyor belt(array).
Example 1 :
N=8 and arr = [4,5,0,1,9,0,5,0].
There are 3 empty packets in the given set. These 3 empty packets represented as O
should be pushed towards the end of the array
Input :
8 Value of N
[4,5,0,1,9,0,5,0] Element of arr[O] to arr[N-1],While input each element is separated by
newline
Output:
4 5 1 9 5 0 0 0
Example 2:
Input:
6 Value of N.
[6,0,1,8,0,2] Element of arr[0] to arr[N-1], While input each element is separated by
newline
Output:
6 1 8 2 0 0
C
C++
Java
Python
Run
#include <stdio.h>
int main()
{
int n, j = 0;
scanf("%d", &n);
int a[n];
for (int i = 0; i < n; i++)
{
scanf("%d", &a[j]);
if (a[j] != 0)
{
j++;
}
}
for (int i = 0; i < j; i++)
{
printf("%d ", a[i]);
}
return 0;
}
TCS NQT Coding Question 2023 September Day 1 Slot 1
Problem Statement
Joseph is learning digital logic subject which will be for his next semester. He usually
tries to solve unit assignment problems before the lecture. Today he got one tricky
question. The problem statement is “A positive integer has been given as an input.
Convert decimal value to binary representation. Toggle all bits of it after the most
significant bit including the most significant bit. Print the positive integer value after
toggling all bits”.
Constrains-
1<=N<=100
Example 1:
Input :
10 -> Integer
Output :
5 -> result- Integer
Explanation:
Binary representation of 10 is 1010. After toggling the bits(1010), will get 0101 which
represents “5”. Hence output will print “5”.
C
C++
Java
Python
Run
#include<stdio.h>
#include<math.h>
int main()
{
int n;
scanf("%d", &n);
int k = (1 << (int)(log2(n) + 1)) - 1;
printf("%d", n ^ k);
return 0;
}
Jack is always excited about sunday. It is favourite day, when he gets to play all day.
And goes to cycling with his friends.
So every time when the months starts he counts the number of sundays he will get to
enjoy. Considering the month can start with any day, be it Sunday, Monday…. Or so on.
Count the number of Sunday jack will get within n number of days.
/ 535
End of Document
161