Problem-solving skills are highly valued in the tech industry, and coding interviews often include questions that test your ability to think critically and solve complex problems. These questions not only assess your knowledge of programming concepts but also evaluate your problem-solving techniques and strategies. In this article, we will explore the top 10 problematic logical challenging interview questions to help you prepare for your next coding interview.
- Reverse a String: Write a function that takes a string as input and returns the reverse of that string. For example, if the input is “hello”, the output should be “olleh”.
function reverseString(str: string): string {
let reversed = '';
for (let i = str.length - 1; i >= 0; i--) {
reversed += str[i];
}
return reversed;
}
console.log(reverseString('hello')); // Output: olleh
- Check Palindrome: Write a function that checks if a given string is a palindrome. A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward. For example, “level” is a palindrome.
function isPalindrome(str: string): boolean {
const reversed = reverseString(str);
return str === reversed;
}
console.log(isPalindrome('level')); // Output: true
console.log(isPalindrome('hello')); // Output: false
- Fibonacci Series: Write a function that prints the Fibonacci series up to a given number. The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones.
function fibonacciSeries(n: number): number[] {
const series = [0, 1];
for (let i = 2; i < n; i++) {
series[i] = series[i - 1] + series[i - 2];
}
return series;
}
console.log(fibonacciSeries(10)); // Output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
- Two Sum: Given an array of integers and a target number, find two numbers in the array that add up to the target. Return the indices of the two numbers as an array.
function twoSum(nums: number[], target: number): number[] {
const map = new Map<number, number>();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (map.has(complement)) {
return [map.get(complement), i];
}
map.set(nums[i], i);
}
return [];
}
console.log(twoSum([2, 7, 11, 15], 9)); // Output: [0, 1]
- Reverse Linked List: Given the head of a singly linked list, reverse the list and return the new head.
class ListNode {
val: number;
next: ListNode | null;
constructor(val?: number, next?: ListNode | null) {
this.val = val === undefined ? 0 : val;
this.next = next === undefined ? null : next;
}
}
function reverseLinkedList(head: ListNode | null): ListNode | null {
let prev = null;
let current = head;
while (current) {
const next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
}