Generic IdentityProblem Statement'Write a generic function identity<T>(arg: T): T that returns the argument provided.'Approach 1. Define a type parameter `<T>`. 2. Use `T` for the parameter type and return type. Solution`function identity<T>(arg: T): T { return arg;
First Element of ArrayProblem Statement'Write a generic function getFirst<T>(arr: T[]): T that returns the first element of an array.'Approach 1. The parameter is an array of `T`. 2. The return is a single `T`. Solution`function getFirst<T>(arr: T[]): T { return arr[0];
Last ElementProblem Statement'Write a generic function getLast<T>(arr: T[]): T | undefined.'Approach 1. Access `arr[arr.length - 1]`. Solution`function getLast<T>(arr: T[]): T | undefined { return arr[arr.length - 1];
Generic ConstraintProblem Statement'Write a function logLength<T extends { length: numberApproach 1. Use `extends` to ensure `T` has a `length` property. Solution`function logLength<T extends { length: number
Generic InterfaceProblem Statement'Create a generic interface Box<T> with a property content of type T.'Approach 1. Combine `interface` with `<T>`. Solution`interface Box<T> { content: T;
Generic ClassProblem Statement'Create a generic class Queue<T> with methods push(item: T) and pop(): T | undefined.'Approach 1. Use a private array of type `T[]`. Solution`class Queue<T> { private data: T[] = []; push(item: T) { this.data.push(item);
Generic WrapperProblem Statement'Write a function createPair<T, U>(a: T, b: U): [T, U] that returns a tuple.'Approach 1. Use multiple type parameters separated by commas. Solution`function createPair<T, U>(a: T, b: U): [T, U] { return [a, b];
KeyOf ConstraintProblem Statement'Write a function getProperty<T, K extends keyof T>(obj: T, key: K) that returns the value of the key from the object.'Approach 1. `K` must be one of the keys of `T`. Solution`function getProperty<T, K extends keyof T>(obj: T, key: K) { return obj[key];
Generic DefaultProblem Statement'Create a generic interface Container<T = string> where the default type is string.'Approach 1. Use `= type` after the type parameter. Solution`interface Container<T = string> { value: T;
Generic FilterProblem Statement'Write a generic filter function myFilter<T>(arr: T[], predicate: (item: T) => boolean): T[].'Approach 1. The predicate takes `T` and returns `boolean`. Solution`function myFilter<T>(arr: T[], predicate: (item: T) => boolean): T[] { const result: T[] = []; for (const item of arr) { if (predicate(item)) result.push(item);
Generic MergeProblem Statement'Write a function merge<T, U>(a: T, b: U): T & U that merges two objects.'Approach 1. Return type is the intersection of `T` and `U`. Solution`function merge<T, U>(a: T, b: U): T & U { return { ...a, ...b
Simple Generic StackProblem Statement'Implement a Stack<T> class with peek() method.'Approach 1. `peek` returns the last element without removing it. Solution`class Stack<T> { private items: T[] = []; push(item: T) { this.items.push(item);
Swap Tuple ValuesProblem Statement'Write a generic function swap<T, U>(pair: [T, U]): [U, T].'Approach 1. Return a new tuple with swapped types. Solution`function swap<T, U>(pair: [T, U]): [U, T] { return [pair[1], pair[0]];
Wrap in ArrayProblem Statement'Write a function wrap<T>(item: T): T[] that puts an item into an array.'Approach 1. Simply return `[item]`. Solution`function wrap<T>(item: T): T[] { return [item];
Find ItemProblem Statement'Write a function findItem<T>(arr: T[], target: T): number (index or -1).'Approach 1. Use `indexOf`. Solution`function findItem<T>(arr: T[], target: T): number { return arr.indexOf(target);
Generic Result typeProblem Statement'Define a type Result<T> that is either { success: true, data: TApproach 1. Use a discriminated union. Solution`type Result<T> = | { success: true; data: T
Pluck PropertyProblem Statement'Write a function pluck<T, K extends keyof T>(arr: T[], key: K): T[K][].'Approach 1. It takes an array of objects and returns an array of property values. Solution`function pluck<T, K extends keyof T>(arr: T[], key: K): T[K][] { return arr.map(item => item[key]);
Dictionary LookupProblem Statement'Write a function lookup<T>(dict: Record<string, T>, key: string): T | undefined.'Approach 1. Use the `Record` utility. Solution`function lookup<T>(dict: Record<string, T>, key: string): T | undefined { return dict[key];
State ManagerProblem Statement'Create a class State<T> with get() and set(val: T) methods.'Approach 1. Store initial value in a private property. Solution`class State<T> { constructor(private value: T) {
Generic Function PropProblem Statement'Write a function callWith<T, R>(val: T, fn: (arg: T) => R): R.'Approach 1. Passes `val` to `fn` and returns the result. Solution`function callWith<T, R>(val: T, fn: (arg: T) => R): R { return fn(val);