- Typescript Daily
- Posts
- Quick Read - Frontend Engineering Interview - Simple problems and their solutions
Quick Read - Frontend Engineering Interview - Simple problems and their solutions
Take a break from your routine, grab your favourite music, scroll through these simple but useful interview problems!
Welcome to today's TypeScript edition, where we immerse ourselves in 8 hands-on problem-solving challenges crucial for TypeScript interviews. From reversing strings to implementing data structures like queues and binary search trees, we'll navigate through a myriad of real-world scenarios that elevate your TypeScript proficiency.
These questions delve deep into fundamental algorithms, data structures, and logic, offering practical insights into tackling TypeScript interviews with confidence. Each solution is a step towards honing your problem-solving prowess, empowering you to decode complexities and flourish in the dynamic landscape of TypeScript development. While this article doesn’t necessarily focus on all the edge cases and perfect solutions, it touches upon the approach that can be taken to achieve the correct solutions.
Let's embark on this journey of exploration and mastery, unlocking the potential within each problem-solving challenge.
1. Reverse a String using TypeScript.
Approach: Use TypeScript's string manipulation capabilities to reverse the string.
Solution:
function reverseString(str: string): string {
return str.split('').reverse().join('');
}
const reversed = reverseString('Typescript');
console.log(reversed); // Outputs: tpircsepyT
2. Implement a Queue using TypeScript.
Approach: Create a class with enqueue, dequeue, and peek methods to manage the queue.
Solution:
class Queue<T> {
private items: T[] = [];
enqueue(item: T): void {
this.items.push(item);
}
dequeue(): T | undefined {
return this.items.shift();
}
peek(): T | undefined {
return this.items[0];
}
}
const queue = new Queue<number>();
queue.enqueue(5);
queue.enqueue(10);
console.log(queue.peek()); // Outputs: 5
console.log(queue.dequeue()); // Outputs: 5
3. Write a function to check if a number is prime in TypeScript.
Approach: Use a loop to check if the number is divisible by any number other than 1 and itself.
Solution:
function isPrime(num: number): boolean {
if (num <= 1) return false;
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) return false;
}
return true;
}
console.log(isPrime(7)); // Outputs: true
console.log(isPrime(10)); // Outputs: false
4. Create a TypeScript function to find the maximum number in an array.
Approach: Iterate through the array to find the maximum number.
Solution:
function findMax(arr: number[]): number {
let max = arr[0];
for (let i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
console.log(findMax([3, 7, 2, 9, 4])); // Outputs: 9
5. Implement a Binary Search Tree (BST) in TypeScript.
Approach: Define a Node class and a BST class with insertion, search, and deletion methods.
Solution:
class Node {
value: number;
left: Node | null;
right: Node | null;
constructor(value: number) {
this.value = value;
this.left = null;
this.right = null;
}
}
class BinarySearchTree {
root: Node | null;
constructor() {
this.root = null;
}
// Implement insertion, search, deletion methods...
// Code for insertion, search, and deletion not provided for brevity.
}
// Usage
const bst = new BinarySearchTree();
// Perform BST operations...
6. Build a TypeScript function to check for palindrome strings.
Approach: Reverse the string and compare it with the original string.
Solution:
function isPalindrome(str: string): boolean {
const reversed = str.split('').reverse().join('');
return str === reversed;
}
console.log(isPalindrome('madam')); // Outputs: true
console.log(isPalindrome('hello')); // Outputs: false
7. Write a TypeScript program to sort an array of numbers.
Approach: Utilize TypeScript's built-in sorting methods or implement popular sorting algorithms.
Solution:
function sortNumbers(arr: number[]): number[] {
return arr.sort((a, b) => a - b); // Using built-in method for simplicity
}
console.log(sortNumbers([5, 2, 9, 1, 5])); // Outputs: [1, 2, 5, 5, 9]
8. Implement a Linked List in TypeScript.
Approach: Create a Node class and a LinkedList class with operations like insertion, deletion, and traversal.
Solution:
class Node<T> {
value: T;
next: Node<T> | null;
constructor(value: T) {
this.value = value;
this.next = null;
}
}
class LinkedList<T> {
head: Node<T> | null;
constructor() {
this.head = null;
}
// Implement insertion, deletion, traversal methods...
// Code for insertion, deletion, and traversal not provided for brevity.
}
// Usage
const linkedList = new LinkedList<number>();
// Perform linked list operations...
Happy Coding!
Reply