0% found this document useful (0 votes)
3 views

Top Coding Interview Questions Java

The document provides a list of over 30 coding questions commonly asked in MNC interviews, along with explanations and code snippets in C++, Python, and Java. Each question covers fundamental programming concepts such as reversing a number, finding Fibonacci series, checking for prime numbers, and implementing sorting algorithms. The document serves as a comprehensive guide for candidates preparing for technical interviews.

Uploaded by

Rajshekar Pujari
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Top Coding Interview Questions Java

The document provides a list of over 30 coding questions commonly asked in MNC interviews, along with explanations and code snippets in C++, Python, and Java. Each question covers fundamental programming concepts such as reversing a number, finding Fibonacci series, checking for prime numbers, and implementing sorting algorithms. The document serves as a comprehensive guide for candidates preparing for technical interviews.

Uploaded by

Rajshekar Pujari
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 71

Top 30+ Coding Questions for MNCS Interview By Techie

CodeBuddy

Q. Write a code to reverse a number?


C++:

Python:

Java:
Explanation:
 The logic is the same across all languages:
1. Extract the last digit of the number using %
10.
2. Multiply the reversed number by 10 and add
the extracted digit.
3. Divide the original number by 10 to remove
the last digit.
4. Repeat until the original number becomes 0.

Q. Write the code to find the Fibonacci series upto the


nth term?
C++:

Python:

Java:
Explanation:
 Fibonacci series is a sequence where each
number is the sum of the two preceding ones,
starting from 0 and 1. So, the series goes: 0, 1, 1,
2, 3, 5, 8, and so on.
 In all three languages:
1. Initialize two variables a and b with 0 and 1,
respectively.
2. Loop n times to print the Fibonacci sequence.
3. In each iteration, calculate the next number as
the sum of a and b, then update a and b.
Q. Write code of Greatest Common Divisor?

C++:

Python:

Java:
Explanation:
 GCD (Greatest Common Divisor) is the largest
number that divides both numbers without leaving
a remainder.
 The Euclidean algorithm is used:
1. Divide the larger number by the smaller one.
2. Replace the larger number with the remainder
of the division.
3. Repeat the process until the remainder is 0.
The non-zero number is the GCD.

Q. Write code to Check if two strings are Anagram or


not?
C++:

Python:
Java:
Explanation:
 Anagram: Two strings are anagrams if one string
can be rearranged to form the other. Both strings
must have the same characters with the same
frequency.
 Approach:
1. Check if the lengths of both strings are the
same. If not, they cannot be anagrams.
2. Sort the characters of both strings.
3. Compare the sorted versions. If they are equal,
the strings are anagrams; otherwise, they are
not.

Q. Write code Check if the given string is Palindrome or


not?

C++:
Python:

Java:
Explanation:
 Palindrome: A string is a palindrome if it reads the
same forwards and backwards (e.g., "madam",
"racecar").
 Approach:
1. Compare the first and last characters of the
string.
2. Continue comparing characters from the
beginning and end of the string moving toward
the center.
3. If all characters match, the string is a
palindrome; otherwise, it is not.
In Python, the approach is simplified using slicing s[::-1]
to reverse the string and compare it directly.

Q. Write a code for bubble sort?

C++:
Python:

Java:
Explanation:
 Bubble Sort works by repeatedly swapping
adjacent elements if they are in the wrong order.
 It compares each pair of adjacent elements and
"bubbles up" the largest element to the end of the
list in each pass.
 The process repeats for all elements until no more
swaps are needed.
Time Complexity:
 Best case: O(n) (when the array is already sorted)
 Worst and average case: O(n^2)
Q. Write a code for binary search?

C++:

Python:
Java:
Explanation:
 Binary Search works by dividing the array into
two halves and comparing the target element with
the middle element.
o If the middle element is equal to the target,
return the index.
o If the middle element is greater than the
target, search in the left half.
o If the middle element is smaller than the
target, search in the right half.
 The array should be sorted for binary search to
work.
Time Complexity:
 Best case: O(1)
 Worst and average case: O(log n)

Q. kadane algorithm?
Kadane's Algorithm is used to find the maximum sum
of a contiguous subarray in an array of integers. It
solves the problem in linear time, making it an efficient
solution for this classic problem.

C++:
Python:
Explanation:
 Kadane's Algorithm works by iterating through
the array and maintaining two variables:
1. max_ending_here: This keeps track of the
maximum sum of the subarray ending at the
current index.
2. max_so_far: This stores the overall maximum
sum encountered so far.
 For each element in the array:
1. Add the element to max_ending_here.
2. If max_ending_here becomes negative, reset it
to 0 (because any subarray with a negative
sum would only decrease the sum of
subsequent elements).
3. Keep track of the maximum value of
max_ending_here encountered so far in
max_so_far.
Time Complexity:
 Time Complexity: O(n), where n is the number of
elements in the array.
 Space Complexity: O(1) (since no extra space is
used except for variables).

Q. How is the merge sort algorithm implemented?


Merge Sort is a divide-and-conquer algorithm that
divides the array into two halves, recursively sorts each
half, and then merges the sorted halves. It efficiently
sorts arrays in O(n log n) time.
C++:
Python:
Java:
Explanation:
1. Divide: Split the array into two halves.
2. Conquer: Recursively sort the two halves.
3. Merge: Combine the two sorted halves to get a
fully sorted array.
Time Complexity:
 Best, Average, and Worst Case: O(n log n)
Merge Sort performs consistently across different
cases since it always splits and merges the array in
O(n log n) time.
Space Complexity:
 O(n) due to the temporary arrays used for merging.

Q. Write to code to check whether a given year is leap


year or not?
Leap Year Criteria:
A year is a leap year if:
 It is divisible by 4.
 However, if it is divisible by 100, it is not a leap
year, unless it is also divisible by 400.
C++ Code:
Python:
Java:
Explanation:
1.Input the Year: The user inputs the year they
want to check.
2.Check Leap Year:
o If the year is divisible by 4, check if it's also
divisible by 100.
o If it's divisible by 100, check if it's also divisible
by 400.
3.Output: Print whether the year is a leap year or
not based on the checks.
Example:
 Input: 2024
 Output: 2024 is a leap year.
 Input: 1900
 Output: 1900 is not a leap year.

Q. Write a code to replace a substring in a string?


C++:
Python:

Java:
Explanation:
1.Input: The user inputs the original string, the
substring they want to replace, and the
replacement substring.
2. Replace: The program finds all occurrences of the
substring in the original string and replaces them
with the specified replacement.
3.Output: The modified string is printed.
Example:
 Input:
o Original String: Hello, world!
o Substring to Replace: world
o Replacement Substring: there
 Output: Modified string: Hello, there!
Q. Write a code to replace each element in an array by
its rank in the array?
Explanation:
1. Rank Calculation: Each element in the array is
replaced by its rank, where the rank is determined
by the position of the element in the sorted array
(1-based index).
2. Handling Duplicates: Elements with the same
value should have the same rank.
C++ Code:
Python:

Java:
Explanation of the Code:
1. Input Array: The array is defined with some
integers.
2. Sorting and Unique Elements: Create a sorted
version of the array and maintain unique elements
for rank calculation.
3. Mapping Ranks: Create a mapping of each
unique element to its rank.
4. Replace Elements: Replace each element in the
original array with its corresponding rank from the
map.
5. Output: The modified array with ranks is printed.
Example:
 Input: [40, 10, 20, 30, 30]
 Output: [4, 1, 2, 3, 3] (where 10 is rank 1, 20 is
rank 2, 30 is rank 3, and 40 is rank 4)

Q. Write a code to find circular rotation of an


array by K positions?
Explanation:
1. Circular Rotation: For an array of size NNN,
rotating it KKK times means that the last KKK
elements move to the front of the array, and the
rest shift to the right.
2. Effective Rotation: If K is greater than N, we can
take K mod N to find the effective number of
rotations.
C++ code:
Python:
Java:

Explanation of the Code:


1. Input Array: The array is defined with integers,
and KKK is specified as the number of positions to
rotate.
2. Effective Rotations: Calculate K mod N to handle
cases where KKK is greater than the size of the
array.
3. Reversal Approach:
o Reverse the entire array.
o Reverse the first KKK elements.
o Reverse the remaining N−KN-KN−K elements.
4. Output: The modified array after rotation is
printed.
Example:
 Input: [1, 2, 3, 4, 5, 6, 7], K=3K = 3K=3
 Output: [5, 6, 7, 1, 2, 3, 4] (after rotating the array
3 positions to the right)

Q. Write a code to find the factorial of a number?


Explanation:
1. Factorial Definition: The factorial of a non-
negative integer n is the product of all positive
integers less than or equal to n (denoted as n!). For
example, 5!=5×4×3×2×1=120.
2. Base Case: The factorial of 000 is defined as 111.
C++:
Python:

Java:
Explanation of the Code:
1. Input: The user is prompted to enter a non-
negative integer.
2. Factorial Function:
o A recursive function is defined to calculate the
factorial.
o The base case handles the factorial of 000.
o The recursive case multiplies nnn by the
factorial of n−1n-1n−1.
3. Output: The calculated factorial is displayed.
Example:
 Input: 5
 Output: Factorial of 5 is 120
Q. Write a code to find prime number or not?
Explanation:
1. Prime Definition: A prime number is a natural
number greater than 1 that has no positive divisors
other than 1 and itself.
2. Checking Primality:
o If the number is less than or equal to 1, it is
not prime.
o If the number is 2, it is prime (the only even
prime).
o For numbers greater than 2, check for factors
from 2 to the square root of the number.
C++:
Python:
Java:
Explanation of the Code:
1. Input: The user is prompted to enter a positive
integer.
2. Prime Check Function:
o Check if the number is less than or equal to 1
(not prime).
o Check if the number is 2 (prime).
o Exclude even numbers greater than 2.
o For odd numbers, check divisibility from 3 up
to the square root of the number.
3. Output: The result indicating whether the number
is prime or not is displayed.
Example:
 Input: 7
 Output: 7 is a prime number.

Q. Write a program to find the sum of Natural Numbers


using Recursion?
Explanation:
1. Natural Numbers: Natural numbers are the set of
positive integers starting from 1 (i.e., 1, 2, 3, ...).
2. Recursive Function: The recursive function will
call itself with a reduced value until it reaches the
base case (when the number is 0).
C++:
Python:

Java:
Explanation of the Code:
1. Input: The user is prompted to enter a positive
integer.
2. Sum Function:
o The function checks if the input number nnn is
0 (base case). If so, it returns 0.
o If nnn is greater than 0, it adds nnn to the
result of the function called with n−1n-1n−1
(recursive case).
3. Output: The total sum of natural numbers up to
the given number is displayed.
Example:
 Input: 5
 Output: The sum of natural numbers up to 5 is 15
(since 1+2+3+4+5=151 + 2 + 3 + 4 + 5 =
151+2+3+4+5=15)

Q. Write a program for Binary to Decimal to conversion?

Explanation:
1. Binary Number: A binary number is represented
using only 0s and 1s.
2. Decimal Conversion: To convert a binary number
to decimal, you can use the formula:

where b is the binary digit and n is its position


(starting from 0).
C++:
Python:

Java:
Explanation of the Code:
1. Input: The user is prompted to enter a binary
number as a string.
2. Conversion Function:
o The function iterates through each bit of the
binary string from the least significant bit
(rightmost) to the most significant bit
(leftmost).
o It checks if the bit is '1'. If it is, it calculates its
decimal value by raising 2 to the power of its
position and adds it to the total decimal value.
3. Output: The decimal equivalent of the binary
number is displayed.
Example:
 Input: 1011
 Output: Decimal equivalent: 11 (since
1×2^3+0×2^2+1×2^1+1×2^0=8+0+2+1=11
Q. Write a program to check whether a character is a
vowel or consonant?
Explanation:
1. Vowels: The vowels in the English language are A,
E, I, O, U (both uppercase and lowercase).
2. Consonants: All other alphabetical characters that
are not vowels are consonants.
C++ Code:

Python:
Java:

Explanation of the Code:


1. Input: The user is prompted to enter a character.
2. Check Function:
o The function converts the character to
lowercase to simplify the comparison.
o It checks if the character is a vowel by seeing
if it is one of 'a', 'e', 'i', 'o', or 'u'.
3. Output: The program informs the user if the
character is a vowel or a consonant. It also checks
if the input is a valid alphabet character.
Example:
 Input: e
 Output: e is a vowel.
 Input: C
 Output: C is a consonant.
Q. Write a code to find Find the ASCII value of a
character?
Explanation:
1. ASCII (American Standard Code for
Information Interchange): Each character is
represented by a unique integer value. For
example, the ASCII value of 'A' is 65 and 'a' is 97.
2. Finding ASCII Value: In programming, you can
find the ASCII value of a character by simply
casting it to an integer or using a specific function.
C++ Code:
Python:

Java:

Explanation of the Code:


1. Input: The user is prompted to enter a character.
2. Finding ASCII Value:
o C++: The character is cast to an int to get its
ASCII value.
o Python: The ord() function is used to get the
ASCII value.
o Java: The character is cast to an int.
3. Output: The program displays the ASCII value of
the entered character.
Example:
 Input: A
 Output: The ASCII value of 'A' is 65
 Input: a
 Output: The ASCII value of 'a' is 97

Q. Write a code to Print the smallest element of the


array?
Explanation:
1. Smallest Element: The smallest element in an
array is the minimum value among its elements.
2. Approach:
o Initialize a variable to hold the smallest
element, starting with the first element of the
array.
o Iterate through the array, updating the
variable whenever a smaller element is found.
C++ Code:
Python:

Java:
xplanation of the Code:
1. Input: The user is prompted to enter the number
of elements in the array, followed by the elements
themselves.
2. Finding the Smallest Element:
o The smallest element is initialized with the
first element of the array.
o The program iterates through the array,
comparing each element with the current
smallest value and updating it if a smaller
value is found.
3. Output: The smallest element in the array is
displayed.
Example:
 Input:

Output: The smallest element in the array is: 2

Q. write a program to reverse a number?


Explanation:
1.Reverse a Number: The reverse of a number
is obtained by reading its digits from the last
to the first.
2.Approach:
o Initialize a variable to hold the reversed
number.
o Use a loop to extract the last digit of the
number and build the reversed number.
C++:
Python:

Java:
Explanation of the Code:
1.Input: The user is prompted to enter a
number.
2.Reversing the Number:
o The last digit of the number is extracted
using the modulo operator (%).
o The reversed number is built by
multiplying the current reversed number
by 10 and adding the extracted digit.
o The last digit of the original number is
removed by performing integer division
by 10.
3.Output: The reversed number is displayed.
Example:
 Input: 12345
 Output: The reversed number is: 54321

Q. Write a Program to Find out the Sum of Digits


of a Number?
Explanation:
1. Sum of Digits: The sum of the digits of a number
is calculated by repeatedly extracting the last digit
and adding it to a sum variable.
2.Approach:
o Initialize a variable to hold the sum of digits.
o Use a loop to extract each digit by taking the
modulo of 10 and then divide the number by
10 to remove the last digit.
C++:

Python:

Java:
Explanation of the Code:
1. Input: The user is prompted to enter a number.
2. Calculating the Sum of Digits:
o The last digit of the number is extracted using
the modulo operator (%).
o The extracted digit is added to the sum.
o The last digit is removed from the number by
performing integer division by 10.
3. Output: The program displays the sum of the
digits.
Example:
 Input: 1234
 Output: The sum of the digits is: 10

Q. Write a Program to Find out the Power of a Number?


Explanation:
1. Power of a Number: The power of a number aaa
raised to nnn (denoted as ana^nan) is the result of
multiplying aaa by itself nnn times.
2. Approach:
o Use a loop to multiply the base number by
itself for the number of times specified by the
exponent.
C++:

Python:

Java:
Explanation of the Code:
1. Input: The user is prompted to enter the base and
exponent.
2. Calculating Power:
o A loop iterates for the number of times
specified by the exponent, multiplying the
base each time to get the result.
3. Output: The program displays the result of raising
the base to the specified power.
Example:
 Input:

Output: 3 raised to the power 4 is: 81


Q. Write a Program to Find the Prime Factors of a
Number?
Explanation:
1. Prime Factors: Prime factors of a number are the
prime numbers that divide the number exactly,
without leaving a remainder.
2. Approach:
o Start with the smallest prime number (2).
o Check if it divides the number. If it does, keep
dividing the number by that prime and store it
as a factor.
o Move to the next number and repeat the
process until the number is reduced to 1.
C++:

Python:
Java:

Explanation of the Code:


1. Input: The user is prompted to enter a number.
2. Finding Prime Factors:
o A loop iterates from 2 up to the number itself.
o For each potential factor, a while loop checks if
it divides the number.
o If it does, the factor is printed, and the number
is divided by that factor.
3. Output: The program displays all the prime factors
of the number.
Example:
 Input: 60
 Output: Prime factors of 60 are: 2 3 5

You might also like