Problem Statement
Logical reasoning questions are often included in technical interviews to assess a candidate’s problem-solving abilities, critical thinking skills, and ability to analyze complex scenarios. These questions require a logical approach and can sometimes be quite challenging to solve under time constraints. In this guide, we will explore some of the most problematic logical interview questions and provide step-by-step solutions with code examples.
Problem 1: Finding the Missing Number
You are given an array of n-1 integers that range from 1 to n. The array’s elements are randomly shuffled, and one number is missing. How would you find the missing number?
Solution:
function findMissingNumber(arr) {
const sumOfArray = arr.reduce((acc, curr) => acc + curr);
const actualSum = ((arr.length + 1) * (arr.length + 2)) / 2;
return actualSum - sumOfArray;
}
Problem 2: Reversing a String
Write a function that reverses a given string in-place without using any additional memory.
Solution:
function reverseString(str) {
let leftIndex = 0;
let rightIndex = str.length - 1;
while (leftIndex < rightIndex) {
const temp = str[leftIndex];
str[leftIndex] = str[rightIndex];
str[rightIndex] = temp;
leftIndex++;
rightIndex--;
}
return str;
}
Problem 3: Validating Parentheses
Given a string containing only parentheses, write a function to determine if the parentheses are valid.
Solution:
function isValidParentheses(string) {
const stack = [];
const openingParentheses = ['(', '{', '['];
const closingParentheses = [')', '}', ']'];
for (let i = 0; i < string.length; i++) {
if (openingParentheses.includes(string[i])) {
stack.push(string[i]);
} else if (closingParentheses.includes(string[i])) {
if (stack.length === 0) return false;
const topElement = stack.pop();
const correspondingOpening = openingParentheses[closingParentheses.indexOf(string[i])];
if (topElement !== correspondingOpening) {
return false;
}
}
}
return stack.length === 0;
}
Problem 4: Finding the Second Largest Number
Write a function that finds the second-largest number in an array of integers.
Solution:
function findSecondLargest(arr) {
let first = Number.NEGATIVE_INFINITY;
let second = Number.NEGATIVE_INFINITY;
for (let i = 0; i < arr.length; i++) {
if (arr[i] > first) {
second = first;
first = arr[i];
} else if (arr[i] > second && arr[i] !== first) {
second = arr[i];
}
}
return second;
}
Problem 5: Finding the Longest Palindrome
Given a string, find the longest palindrome substring.
Solution:
function longestPalindrome(str) {
if (str === null || str.length < 1) return '';
let start = 0;
let end = 0;
for (let i = 0; i < str.length; i++) {
const len1 = expandAroundCenter(str, i, i);
const len2 = expandAroundCenter(str, i, i + 1);
const len = Math.max(len1, len2);
if (len > end - start) {
start = i - Math.floor((len - 1) / 2);
end = i + Math.floor(len / 2);
}
}
return str.slice(start, end + 1);
}
function expandAroundCenter(str, left, right) {
let L = left;
let R = right;
while (L >= 0 && R < str.length && str[L] === str[R]) {
L--;
R++;
}
return R - L - 1;
}
Problem 6: Finding the Kth Smallest Number
Write a function that finds the Kth smallest number in an unsorted array.
Solution:
function findKthSmallest(arr, k) {
const sortedArr = arr.sort((a, b) => a - b);
return sortedArr[k - 1];
}
Conclusion
By understanding the problem-solving techniques and practicing logical reasoning, you can effectively tackle challenging logical interview questions. Remember to approach the problems systematically, break them down into smaller tasks, and utilize code examples to implement your solutions. With enough preparation and practice, you’ll be ready to ace your next interview!
We hope you found this guide helpful in preparing for your upcoming interviews. Good luck!
Categories: Interview Preparation, Problem Solving, Coding