**
Are you looking to ace your next interview and showcase your problem-solving skills? Logical interview questions are commonly asked in technical interviews to assess a candidate’s ability to think critically and come up with innovative solutions. These types of questions require you to apply logic, reasoning, and algorithmic thinking to solve complex problems efficiently.
In this article, we will provide you with a curated list of 50 challenging logical interview questions along with detailed answers and code examples for each question. These questions cover various areas such as puzzles, algorithms, data structures, and mathematical problems. By practicing these questions, you will sharpen your logical thinking and be better prepared for your upcoming interviews.
Without further ado, let’s dive into the world of challenging logical interview questions:
- Reverse a String: Write a Java program to reverse a given string without using any built-in methods or libraries.
public class ReverseString {
public static void main(String[] args) {
String str = "Hello World";
StringBuilder reversedStr = new StringBuilder();
for (int i = str.length() - 1; i >= 0; i--) {
reversedStr.append(str.charAt(i));
}
System.out.println(reversedStr.toString());
}
}
- Check Armstrong Number: Write a Java program to check if a given number is an Armstrong number or not.
public class ArmstrongNumber {
public static void main(String[] args) {
int num = 153;
int originalNum = num;
int result = 0;
while (num != 0) {
int remainder = num % 10;
result += Math.pow(remainder, 3);
num /= 10;
}
if (result == originalNum) {
System.out.println(originalNum + " is an Armstrong number.");
} else {
System.out.println(originalNum + " is not an Armstrong number.");
}
}
}
- Find Missing Number: Given an array of n-1 integers in the range from 1 to n, find the missing number.
public class MissingNumber {
public static void main(String[] args) {
int[] arr = {1, 2, 4, 6, 3, 7, 8};
int n = arr.length + 1;
int sum = n * (n + 1) / 2;
for (int num : arr) {
sum -= num;
}
System.out.println("Missing number: " + sum);
}
}
- Check Palindrome: Write a Java program to check if a given string is a palindrome or not.
public class Palindrome {
public static void main(String[] args) {
String str = "madam";
boolean isPalindrome = true;
for (int i = 0; i < str.length() / 2; i++) {
if (str.charAt(i) != str.charAt(str.length() - 1 - i)) {
isPalindrome = false;
break;
}
}
if (isPalindrome) {
System.out.println(str + " is a palindrome.");
} else {
System.out.println(str + " is not a palindrome.");
}
}
}
- Implement Binary Search: Write a Java program to implement the binary search algorithm on a sorted array.
public class BinarySearch {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int target = 7;
int result = binarySearch(arr, target);
if (result != -1) {
System.out.println("Element found at index " + result);
} else {
System.out.println("Element not found in the array.");
}
}
public static int binarySearch(int[] arr, int target) {
int left = 0;
int 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;
}
}
These were just a few examples from the list of 50 challenging logical interview questions. Each question will push your problem-solving skills to the limit and require you to think creatively. Remember to practice them regularly and understand the underlying concepts to excel in your interviews.
By thoroughly preparing for these types of questions, you will not only showcase your logical thinking skills but also demonstrate your ability to write clean and efficient code. Good luck with your interview preparation!