11. Write a function to convert a callback-based function to a Promise-based one.
Required Input:
A callback function setTimeout that resolves after 2 seconds.
Expected Output:
Callback resolved
Code In Typescript
function promisify(fn: Function): (...args: any[]) => Promise<any> {
// Your logic here
}
Run Code?
Click Run Button to view compiled output
12. Implement a function that returns all subsets of a given set (Power Set).
Required Input:
[1, 2, 3]
Expected Output:
[
[], [ 1 ],
[ 2 ], [ 1, 2 ],
[ 3 ], [ 1, 3 ],
[ 2, 3 ], [ 1, 2, 3 ]
]
Code In Typescript
function powerSet(arr: number[]): number[][] {
// Your logic here
}
Run Code?
Click Run Button to view compiled output
13. Write a function to detect a cycle in a directed graph using the DFS algorithm.
Required Input:
Graph: { 0: [1], 1: [2], 2: [0], 3: [4], 4: [] }
Expected Output:
Cycle detected: true
Code In Typescript
function detectCycle(graph: { [key: number]: number[] }): boolean {
// Your logic here
}
Run Code?
Click Run Button to view compiled output
14. Write a function to calculate the edit distance (Levenshtein distance) between two strings.
Required Input:
"kitten" and "sitting"
Expected Output:
Edit distance: 3
Code In Typescript
function editDistance(str1: string, str2: string): number {
// Your logic here
}
Run Code?
Click Run Button to view compiled output
15. Write a function to convert a deeply nested object into a flat object.
Required Input:
{ a: { b: { c: 1 }, d: 2 }, e: 3 }
Expected Output:
{ 'a.b.c': 1, 'a.d': 2, e: 3 }
Code In Typescript
function flattenObject(obj: any, parentKey: string = "", result: any = {}): any {
// Your logic here
}
Run Code?
Click Run Button to view compiled output
16. Write a function to group elements of an array by a property.
Required Input:
[{ name: "Alice", age: 25 }, { name: "Bob", age: 30 }, { name: "Charlie", age: 25 }]
Expected Output:
{
'25': [ { name: 'Alice', age: 25 }, { name: 'Charlie', age: 25 } ],
'30': [ { name: 'Bob', age: 30 } ]
}
Code In Typescript
function groupByProperty<T, K extends keyof T>(arr: T[], property: K): { [key: string]: T[] } {
// Your logic here
}
Run Code?
Click Run Button to view compiled output
17. Implement a function to calculate the lowest common ancestor in a binary search tree.
Required Input:
Tree: 6 / \ 2 8 / \ / \ 0 4 7 9
Nodes: 2 and 8
Expected Output:
Lowest Common Ancestor: 6
Code In Typescript
class TreeNode {
val: number;
left: TreeNode
Run Code?
Click Run Button to view compiled output
18. Write a program to implement a rate limiter that allows N executions per second.
Required Input:
Function: printMessage, Limit: 2 calls per second.
Expected Output:
Message 1
Message 2
Rate limit exceeded
Code In Typescript
function rateLimiter(fn: Function, limit: number): Function {
// Your logic here
}
Run Code?
Click Run Button to view compiled output
19. Write a program to detect a cycle in a linked list.
Required Input:
Linked List: 1 -> 2 -> 3 -> 4 -> 2 (cycle)
Expected Output:
Cycle detected: true
Code In Typescript
class ListNode {
val: number;
next: ListNode
Run Code?
Click Run Button to view compiled output
20. Write a function to calculate the majority element in an array (an element that appears more than n/2 times).
Required Input:
[2, 2, 1, 1, 1, 2, 2]
Expected Output:
Majority Element: 2
Code In Typescript
function majorityElement(arr: number[]): number {
// Your logic here
}
Run Code?
Click Run Button to view compiled output