# tcs question with code examples

As a software engineer, you may have come across TCS questions that involve coding. These questions are designed to test your programming skills, logical reasoning, and problem-solving abilities. In this article, we will explore some TCS questions with code examples to help you prepare for your next coding challenge.

Question 1:

Write a program to find the largest and smallest numbers in an array of integers.

Solution:

To solve this problem, we can use the following steps:

1. Initialize two variables, min and max, with the first element of the array.
2. Loop through the array and compare each element with min and max.
3. If an element is smaller than the current min value, update min.
4. If an element is larger than the current max value, update max.
5. After the loop, the values of min and max will be the smallest and largest numbers in the array, respectively.

Here's the code in Java:

```public class SmallestLargest {
public static void main(String[] args) {
int[] arr = {4, 2, 7, 1, 8, 5};
int min = arr[0];
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
}
if (arr[i] > max) {
max = arr[i];
}
}
System.out.println("Smallest number: " + min);
System.out.println("Largest number: " + max);
}
}
```

Question 2:

Write a program to find the sum of all even numbers and the product of all odd numbers in an array.

Solution:

To solve this problem, we can use the following steps:

1. Initialize two variables, sum and product, to 0 and 1, respectively.
2. Loop through the array and check if each element is even or odd.
3. If an element is even, add it to sum.
4. If an element is odd, multiply it to product.
5. After the loop, the values of sum and product will be the sum of all even numbers and the product of all odd numbers in the array, respectively.

Here's the code in Python:

```arr = [3, 6, 1, 8, 4, 5]
sum = 0
product = 1
for num in arr:
if num % 2 == 0:
sum += num
else:
product *= num
print("Sum of even numbers:", sum)
print("Product of odd numbers:", product)
```

Question 3:

Write a program to count the number of duplicate elements in an array.

Solution:

To solve this problem, we can use the following steps:

1. Initialize a variable, count, to 0.
2. Loop through the array and check if each element is a duplicate.
3. To check for duplicates, compare each element with all other elements in the array using nested loops.
4. If an element is found to be a duplicate, increment count by 1.
5. After the loop, count will be the number of duplicate elements in the array.

Here's the code in C++:

```#include <iostream>
using namespace std;
int main() {
int arr[] = {2, 4, 6, 8, 2, 3, 6, 4};
int size = sizeof(arr) / sizeof(arr[0]);
int count = 0;
for (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; j++) {
if (arr[i] == arr[j]) {
count++;
break;
}
}
}
cout << "Number of duplicate elements: " << count << endl;
return 0;
}
```

These are just a few examples of TCS questions with code solutions. When preparing for a coding challenge, it is important to practice implementing algorithms and data structures using programming languages of your choice. Remember to code efficiently, optimize your algorithms, and debug your code thoroughly. Good luck!

Sure! Let's dive deeper into the concepts we covered in the previous examples.

Finding the smallest and largest numbers in an array:

This is a common problem in programming interviews and tests. The solution involves looping through the array once, and keeping track of the minimum and maximum values found so far. Here are a few important points to remember:

• The initial values of the minimum and maximum variables should be set to the first element of the array. If you set them to 0 or some other value, the solution might not work if the array contains negative numbers.
• If the array has only one element, that element is both the minimum and maximum value.
• Make sure to loop through the entire array. If you exit the loop early, you might miss the actual minimum or maximum value.

Summing even numbers and multiplying odd numbers in an array:

This is another common problem that tests your skills in looping, conditionals, and basic arithmetic. Here are a few things to keep in mind:

• The initial values of the sum and product variables should be set to 0 and 1, respectively. If you set them to some other value, the solution might not work correctly.
• Check whether an element is even or odd using the modulo operator (%). If a number is even, modulus by 2 will result in 0. If a number is odd, modulus by 2 will result in 1.
• Remember to multiply each odd number to the product variable, not add. If you add odd numbers, you'll get the wrong answer!
• If the array has only even or odd numbers, you'll need to handle the product or sum variable accordingly.

Counting the number of duplicate elements in an array:

This is a more challenging problem that involves comparing each element in the array with every other element. The solution involves looping through the array using nested loops, and keeping track of the count of duplicate elements found. Here are some tips:

• Use nested loops to compare each element with every other element. The outer loop should iterate through all elements, while the inner loop should start from the next element to avoid counting duplicates twice.
• To avoid comparing an element with itself, start the inner loop from the next element (i + 1).
• When a duplicate element is found, increment the count and exit the inner loop using the break statement. If you don't exit the inner loop, you'll end up counting duplicates multiple times.
• If the array contains multiple copies of the same element, you'll need to handle them correctly in your count.

## Popular questions

Sure, here are 5 TCS coding questions with answers:

Question 1: Write a program in Java to find the second largest number in an array of integers.

Solution:

```public class SecondLargest {
public static void main(String[] args) {
int arr[] = {65, 34, 90, 12, 56, 22};
int max1 = 0;
int max2 = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > max1) {
max2 = max1;
max1 = arr[i];
} else if (arr[i] > max2 && arr[i] != max1) {
max2 = arr[i];
}
}
System.out.println("The second largest number is: " + max2);
}
}
```

Question 2: Write a program in Python to check whether a number is prime or not.

Solution:

```def is_prime(num):
if num <= 1:
return False
for i in range(2, num//2+1):
if num % i == 0:
return False
return True

num = 17
if is_prime(num):
print(num, "is a prime number")
else:
print(num, "is not a prime number")
```

Question 3: Write a program in C++ to reverse the elements of an array.

Solution:

```#include <iostream>
using namespace std;
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
int start = 0, end = size - 1;
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
```

Question 4: Write a program in Java to remove duplicates from an array of integers.

Solution:

```import java.util.Arrays;
public class RemoveDuplicates {
public static void main(String[] args) {
int arr[] = {1, 2, 3, 2, 4, 4, 5};
int size = arr.length;
Arrays.sort(arr);
int[] temp = new int[size];
int j = 0;
for (int i = 0; i < size - 1; i++) {
if (arr[i] != arr[i+1]) {
temp[j++] = arr[i];
}
}
temp[j++] = arr[size-1];
int[] res = new int[j];
for (int i = 0; i < j; i++) {
res[i] = temp[i];
}
System.out.println("Array after removing duplicates: ");
for (int i = 0; i < res.length; i++) {
System.out.print(res[i] + " ");
}
System.out.println();
}
}
```

Question 5: Write a program in Python to find the first N numbers of the Fibonacci sequence.

Solution:

```def fibonacci(n):
fib = [0, 1]
for i in range(2, n):
fib.append(fib[i-1] + fib[i-2])
return fib

n = 10
fib = fibonacci(n)
print("First", n, "Fibonacci numbers:", fib)
```

### Tag

Codetcs

##### Surarchith Midhunakula
My passion for coding started with my very first program in Java. The feeling of manipulating code to produce a desired output ignited a deep love for using software to solve practical problems. For me, software engineering is like solving a puzzle, and I am fully engaged in the process. As a Senior Software Engineer at PayPal, I am dedicated to soaking up as much knowledge and experience as possible in order to perfect my craft. I am constantly seeking to improve my skills and to stay up-to-date with the latest trends and technologies in the field. I have experience working with a diverse range of programming languages, including Ruby on Rails, Java, Python, Spark, Scala, Javascript, and Typescript. Despite my broad experience, I know there is always more to learn, more problems to solve, and more to build. I am eagerly looking forward to the next challenge and am committed to using my skills to create impactful solutions.
Posts created 3177

## markdown embed image with code examples

Begin typing your search term above and press enter to search. Press ESC to cancel.