11. Write a program to find the intersection of two arrays (common elements).
Required Input:
Array 1: [1, 2, 3, 4, 5]
Array 2: [3, 4, 5, 6, 7]
Expected Output:
Intersection: [ 3, 4, 5 ]
Code In Typescript
function arrayIntersection(arr1: number[], arr2: number[]): number[] {
// Your logic here
}
Run Code?
Click Run Button to view compiled output
12. Create a class Person with properties name and age. Add a method to calculate the year of birth.
Required Input:
Name: "Alice"
Age: 30
Expected Output:
Year of Birth: 1995
Code In Typescript
class Person {
name: string;
age: number;
constructor(name: string, age: number) {
// Your logic here
}
calculateYearOfBirth(): number {
// Your logic here
}
}
Run Code?
Click Run Button to view compiled output
13. Write a function that checks if a given number is a prime number.
Required Input:
7
Expected Output:
7 is a prime number
Code In Typescript
function isPrime(num: number): boolean {
// Your logic here
}
Run Code?
Click Run Button to view compiled output
14. Implement a function to rotate an array n times to the right.
Required Input:
[1, 2, 3, 4, 5], Rotations: 2
Expected Output:
Rotated Array: [ 4, 5, 1, 2, 3 ]
Code In Typescript
function rotateArray(arr: number[], rotations: number): number[] {
// Your logic here
}
Run Code?
Click Run Button to view compiled output
15. Write a program to sort an array of objects based on a specific property.
Required Input:
[ { name: "Alice", age: 25 }, { name: "Bob", age: 20 }, { name: "Charlie", age: 30 } ]
Expected Output:
Sorted by age: [
{ name: 'Bob', age: 20 },
{ name: 'Alice', age: 25 },
{ name: 'Charlie', age: 30 }
]
Code In Typescript
// Predefined structure
interface Person {
name: string;
age: number;
}
function sortByAge(people: Person[]): Person[] {
// Your logic here
}
Run Code?
Click Run Button to view compiled output
16. Write a function that calculates the power of a number without using Math.pow.
Required Input:
Base: 2
Exponent: 4
Expected Output:
Result: 16
Code In Typescript
// Predefined structure
function calculatePower(base: number, exponent: number): number {
// Your logic here
}
Run Code?
Click Run Button to view compiled output
17. Create a class Animal with a method speak. Derive a class Dog and override the speak method.
Required Input:
None
Expected Output:
Animal speaks
Dog barks
Code In Typescript
// Predefined structure
class Animal {
speak(): void {
// Your logic here
}
}
class Dog extends Animal {
speak(): void {
// Your logic here
}
}
// Create instances and call the methods
Run Code?
Click Run Button to view compiled output
18. Write a function to find all pairs in an array that sum up to a given target.
Required Input:
Array: [2, 7, 4, 1, 5, 3]
Target: 6
Expected Output:
Pairs: [ [ 2, 4 ], [ 1, 5 ] ]
Code In Typescript
// Predefined structure
function findPairs(arr: number[], target: number): number[][] {
// Your logic here
}
Run Code?
Click Run Button to view compiled output
19. Write a function to generate a random alphanumeric string of a given length.
Required Input:
6
Expected Output:
Random String: A1bC2d
Code In Typescript
// Predefined structure
function generateRandomString(length: number): string {
// Your logic here
}
Run Code?
Click Run Button to view compiled output
20. Create a function to implement binary search on a sorted array.
Required Input:
Array: [1, 3, 5, 7, 9, 11]
Target: 7
Expected Output:
Target found at index: 3
Code In Typescript
// Predefined structure
function binarySearch(arr: number[], target: number): number {
// Your logic here
}
Run Code?
Click Run Button to view compiled output