[go: up one dir, main page]

0% found this document useful (0 votes)
101 views8 pages

Tech Mahindra Coding Questions

The document provides a list of coding questions and solutions for a Tech Mahindra coding exam, including problems like finding the second largest number in an array, checking for palindromes, reversing a linked list, and more. Each problem is accompanied by Java code implementations. Additionally, it references previous questions shared for exam preparation.

Uploaded by

sumityadavit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views8 pages

Tech Mahindra Coding Questions

The document provides a list of coding questions and solutions for a Tech Mahindra coding exam, including problems like finding the second largest number in an array, checking for palindromes, reversing a linked list, and more. Each problem is accompanied by Java code implementations. Additionally, it references previous questions shared for exam preparation.

Uploaded by

sumityadavit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Tech Mahindra Coding Questions

today is my tech mahindra coding exam,give me some questions and answer for refernce

Memory updated
Good luck on your Tech Mahindra coding exam! Here are a few coding questions for practice:

1. Find the Second Largest Number in an Array

Problem: Given an array of integers, find the second largest number.

java

import java.util.Arrays;

public class SecondLargest {


public static int findSecondLargest(int[] arr) {
if (arr.length < 2) return -1; // Handle edge case

Arrays.sort(arr);
for (int i = arr.length - 2; i >= 0; i--) {
if (arr[i] != arr[arr.length - 1]) {
return arr[i];
}
}
return -1;
}

public static void main(String[] args) {


int[] arr = {10, 20, 4, 45, 99, 99};
System.out.println("Second Largest: " + findSecondLargest(arr));
}
}

2. Palindrome Check

Problem: Write a program to check whether a given string is a palindrome.

java

public class PalindromeCheck {


public static boolean isPalindrome(String str) {
int left = 0, right = str.length() - 1;
while (left < right) {
if (str.charAt(left) != str.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 1/8
public static void main(String[] args) {
String testString = "madam";
System.out.println("Is Palindrome: " + isPalindrome(testString));
}
}

3. Reverse a Linked List

Problem: Write a function to reverse a linked list.

java

class Node {
int data;
Node next;

Node(int data) {
this.data = data;
this.next = null;
}
}

public class ReverseLinkedList {


public static Node reverse(Node head) {
Node prev = null, current = head, next = null;

while (current != null) {


next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
}

public static void printList(Node head) {


Node temp = head;
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();
}

public static void main(String[] args) {


Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);

System.out.println("Original List: ");


printList(head);

head = reverse(head);

System.out.println("Reversed List: ");


printList(head);
}
}

4. Find Missing Number in an Array

Problem: You are given an array containing `n` distinct numbers taken from `0, 1, 2, ..., n`. Find
the missing number.

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 2/8
java

public class MissingNumber {


public static int findMissingNumber(int[] nums) {
int n = nums.length;
int totalSum = (n * (n + 1)) / 2;
int arraySum = 0;

for (int num : nums) {


arraySum += num;
}

return totalSum - arraySum;


}

public static void main(String[] args) {


int[] arr = {0, 1, 3};
System.out.println("Missing number: " + findMissingNumber(arr));
}
}

5. Check if Two Strings are Anagrams

Problem: Given two strings, check if they are anagrams of each other.

java

import java.util.Arrays;

public class AnagramCheck {


public static boolean areAnagrams(String s1, String s2) {
if (s1.length() != s2.length()) return false;

char[] arr1 = s1.toCharArray();


char[] arr2 = s2.toCharArray();

Arrays.sort(arr1);
Arrays.sort(arr2);

return Arrays.equals(arr1, arr2);


}

public static void main(String[] args) {


String s1 = "listen";
String s2 = "silent";
System.out.println("Are Anagrams: " + areAnagrams(s1, s2));
}
}

Feel free to modify these as needed!

few days back i asked u some questions give those too

Here are the previous questions I shared with you for your Tech Mahindra preparation:

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 3/8
1. Find Factorial Using Recursion

Problem: Write a program to find the factorial of a given number using recursion.

java

public class Factorial {


public static int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}

public static void main(String[] args) {


int number = 5;
System.out.println("Factorial of " + number + " is: " + factorial(number));
}
}

2. Prime Number Check

Problem: Write a program to check if a given number is prime or not.

java

public class PrimeCheck {


public static boolean isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}

public static void main(String[] args) {


int num = 29;
System.out.println(num + " is prime: " + isPrime(num));
}
}

3. Check for Armstrong Number

Problem: Write a program to check if a given number is an Armstrong number.

java

public class ArmstrongNumber {


public static boolean isArmstrong(int num) {
int original = num, sum = 0;
while (num != 0) {
int digit = num % 10;
sum += Math.pow(digit, 3);
num /= 10;
}
return sum == original;
}

public static void main(String[] args) {


int number = 153;
System.out.println(number + " is Armstrong: " + isArmstrong(number));

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 4/8
}
}

4. Merge Two Sorted Arrays

Problem: Write a program to merge two sorted arrays into one sorted array.

java

import java.util.Arrays;

public class MergeSortedArrays {


public static int[] mergeArrays(int[] arr1, int[] arr2) {
int[] mergedArray = new int[arr1.length + arr2.length];
int i = 0, j = 0, k = 0;

while (i < arr1.length && j < arr2.length) {


if (arr1[i] < arr2[j]) {
mergedArray[k++] = arr1[i++];
} else {
mergedArray[k++] = arr2[j++];
}
}

while (i < arr1.length) {


mergedArray[k++] = arr1[i++];
}

while (j < arr2.length) {


mergedArray[k++] = arr2[j++];
}

return mergedArray;
}

public static void main(String[] args) {


int[] arr1 = {1, 3, 5, 7};
int[] arr2 = {2, 4, 6, 8};
int[] result = mergeArrays(arr1, arr2);

System.out.println("Merged Array: " + Arrays.toString(result));


}
}

5. Find the Largest Element in an Array

Problem: Write a program to find the largest element in an array.

java

public class LargestElement {


public static int findLargest(int[] arr) {
int largest = arr[0];
for (int num : arr) {
if (num > largest) {
largest = num;
}
}
return largest;
}

public static void main(String[] args) {


int[] arr = {10, 20, 30, 40, 50};
System.out.println("Largest Element: " + findLargest(arr));
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 5/8
}
}

These, along with the new ones, should give you a good set of questions for reference. All the best!

see yesterdays chat & reans those

Here are the coding questions I shared with you yesterday for the Tech Mahindra preparation:

1. Find the Sum of Digits of a Number

Problem: Write a program to calculate the sum of the digits of a number.

java

public class SumOfDigits {


public static int sumDigits(int num) {
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
return sum;
}

public static void main(String[] args) {


int number = 12345;
System.out.println("Sum of digits: " + sumDigits(number));
}
}

2. Fibonacci Series Using Recursion

Problem: Write a program to print the Fibonacci series up to `n` terms using recursion.

java

public class FibonacciRecursion {


public static int fibonacci(int n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}

public static void main(String[] args) {


int n = 10;
System.out.println("Fibonacci Series: ");
for (int i = 0; i < n; i++) {
System.out.print(fibonacci(i) + " ");
}
}
}

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 6/8
3. Binary Search

Problem: Implement binary search in a sorted array.

java

public class BinarySearch {


public static int binarySearch(int[] arr, int target) {
int left = 0, right = arr.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) return mid;
if (arr[mid] < target) left = mid + 1;
else right = mid - 1;
}
return -1; // Element not found
}

public static void main(String[] args) {


int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int target = 5;
int result = binarySearch(arr, target);
System.out.println(target + " found at index: " + result);
}
}

4. Reverse a String

Problem: Write a function to reverse a given string.

java

public class ReverseString {


public static String reverse(String str) {
StringBuilder reversed = new StringBuilder(str);
return reversed.reverse().toString();
}

public static void main(String[] args) {


String input = "TechMahindra";
System.out.println("Reversed String: " + reverse(input));
}
}

5. Remove Duplicates from a Sorted Array

Problem: Remove duplicates from a sorted array.

java

public class RemoveDuplicates {


public static int removeDuplicates(int[] arr) {
if (arr.length == 0) return 0;
int index = 1;
for (int i = 1; i < arr.length; i++) {
if (arr[i] != arr[i - 1]) {
arr[index++] = arr[i];
}
}
return index;

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 7/8
}

public static void main(String[] args) {


int[] arr = {1, 1, 2, 2, 3, 4, 4, 5};
int newSize = removeDuplicates(arr);
System.out.println("Array after removing duplicates: ");
for (int i = 0; i < newSize; i++) {
System.out.print(arr[i] + " ");
}
}
}

You now have a wide range of questions for reference. Best of luck with your exam!
ChatGPT can make mistakes. Check important info.

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 8/8

You might also like