Top Coding Interview Questions Java
Top Coding Interview Questions Java
CodeBuddy
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.
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.
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.
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.
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).
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)
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.
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)
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:
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:
Java:
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:
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
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
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:
Python:
Java: