Interview Q&A / Machine Learning
Machine Learning interview questions
Supervised vs unsupervised learning, overfitting, metrics and the classic models, explained simply.
Prefer rapid revision? Practise Machine Learning short interview answers →
- How do you restrict a generic type parameter using extends?
Write T extends Something to say the caller may pass any type, as long as it has at least the shape of Something. Inside the function you can then safely use those members, such as .length or .id.
BeginnerQuick answer1 min read - How do you write a generic class in TypeScript, for example a Stack?
Put the type parameter after the class name, like class Stack<T>, and use T in fields and method signatures. Each instance fixes T when created, so new Stack<number>() only accepts and returns numbers.
BeginnerQuick answer1 min read - How would you model an API response with a generic type?
Define one generic interface such as ApiResponse<T> with a data: T field plus shared fields like status and message. Each endpoint then reuses it with its own data type, for example ApiResponse<User[]>.
BeginnerQuick answer1 min read - How do you write a generic arrow function in a .tsx file?
In .tsx, <T>(x: T) => x is parsed as a JSX tag and fails. Write <T,>(x: T) => x with a trailing comma, or <T extends unknown>, or use a regular function declaration instead.
IntermediateQuick answer1 min read - What is a default type parameter in a generic, and when is it useful?
A default like <T = string> is used when the caller gives no type argument and TypeScript cannot infer one. It keeps simple usage short while still allowing a custom type when needed.
IntermediateQuick answer1 min read - When does TypeScript infer generic type arguments, and when must you pass them yourself?
TypeScript infers type arguments from the values you pass. You must write them explicitly when no argument carries the type, such as an empty array, a function with only a return type, or a call where you want a wider type than the inferred one.
IntermediateQuick answer1 min read - When should you use a generic instead of a union type for a function parameter?
Use a generic when the output type must follow the input type, such as returning the same kind you received. Use a union when the function accepts several kinds and the result does not depend on which one it was.
IntermediateQuick answer1 min read