TypeScript Interview Questions

Top 10 Typescript Interview Problems and Solutions You Must Prepare in 2024

Top 10 Typescript Interview Problems and Solutions You Must Prepare in 2024

  1. Write a function that reverses a string.
function reverseString(str: string): string {
    return str.split('').reverse().join('');
}
  1. Write a function that checks if a given string is a palindrome.
function isPalindrome(str: string): boolean {
    const reversed = str.split('').reverse().join('');
    return str === reversed;
}
  1. Write a function to calculate the factorial of a given number.
function factorial(n: number): number {
    if (n === 0 || n === 1) return 1;
    return n * factorial(n - 1);
}
  1. Write a function to generate the Fibonacci sequence up to a given number.
function fibonacci(n: number): number[] {
    const sequence = [0, 1];
    for (let i = 2; i <= n; i++) {
        sequence[i] = sequence[i - 1] + sequence[i - 2];
    }
    return sequence;
}
  1. Write a function to sort an array of numbers.
function sortArray(arr: number[]): number[] {
    return arr.slice().sort((a, b) => a - b);
}
  1. Write a function to find the first duplicate element in an array.
function findDuplicate(arr: number[]): number | undefined {
    const set = new Set<number>();
    for (const num of arr) {
        if (set.has(num)) {
            return num;
        }
        set.add(num);
    }
    return undefined;
}
  1. Write a function to perform binary search on a sorted array.
function binarySearch(arr: number[], target: number): number {
    let low = 0;
    let high = arr.length - 1;
    while (low <= high) {
        const mid = Math.floor((low + high) / 2);
        if (arr[mid] === target) return mid;
        if (arr[mid] < target) low = mid + 1;
        else high = mid - 1;
    }
    return -1;
}
  1. Given an object, write a function to remove a property by key.
function removeProperty(obj: Record<string, any>, key: string): void {
    delete obj[key];
}
  1. Write a function to deep clone an object.
function deepClone(obj: Record<string, any>): Record<string, any> {
    return JSON.parse(JSON.stringify(obj));
}
  1. Write an asynchronous function that fetches data from an API using Promises and Async/Await.
async function fetchData(url: string): Promise<any> {
    const response = await fetch(url);
    const data = await response.json();
    return data;
}

“Mastering TypeScript Interviews: Top 10 Challenges and How to Solve Them in 2024”
“Crack the Code: Must-Know TypeScript Interview Problems and Solutions for 2024”
“Stay Ahead of the Game: TypeScript Interview Prep with the Top 10 Problems”
“2024 TypeScript Interview Mastery: Essential Problems and Expert Solutions”
“Unlock Success: Nailing TypeScript Interviews – Top 10 Problems Demystified”
“Elevate Your TypeScript Interview Game: 10 Problems You Can’t Afford to Miss”
“TypeScript Triumph: Conquer Interviews with Our Top 10 Problem-Solving Strategies”
“2024 TypeScript Interview Countdown: Your Ultimate Guide to Top 10 Challenges”
“Beyond Basics: Diving into Advanced TypeScript Interview Problems and Solutions”
“TypeScript Interview Excellence: Solutions to Top 10 Coding Challenges in 2024”

Leave a Reply

Your email address will not be published. Required fields are marked *