Challenge - 5 Problems
Builder Pattern Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a generic builder pattern example
What is the output of this TypeScript code using a generic builder pattern?
Typescript
class Builder<T> { private obj: Partial<T> = {}; set<K extends keyof T>(key: K, value: T[K]): this { this.obj[key] = value; return this; } build(): T { return this.obj as T; } } interface Person { name: string; age: number; } const person = new Builder<Person>() .set('name', 'Alice') .set('age', 30) .build(); console.log(person);
Attempts:
2 left
💡 Hint
Look at how the generic Builder class stores and returns the object.
✗ Incorrect
The Builder class collects properties in a Partial object. The set method adds keys and values. The build method casts the partial object to T and returns it. So the final object has both name and age.
❓ Predict Output
intermediate2:00remaining
What error occurs with missing required property?
Given this generic builder pattern code, what error occurs when a required property is missing?
Typescript
class Builder<T> { private obj: Partial<T> = {}; set<K extends keyof T>(key: K, value: T[K]): this { this.obj[key] = value; return this; } build(): T { return this.obj as T; } } interface Car { make: string; year: number; } const car = new Builder<Car>() .set('make', 'Toyota') .build(); console.log(car.year.toString());
Attempts:
2 left
💡 Hint
Check what happens when you access a property that was never set.
✗ Incorrect
The build method casts a Partial to T without checking. The 'year' property is undefined at runtime. Accessing toString() on undefined causes a TypeError.
🔧 Debug
advanced2:00remaining
Identify the bug in this generic builder pattern
What is the bug in this TypeScript generic builder pattern code?
Typescript
class Builder<T> { private obj: T = {} as T; set<K extends keyof T>(key: K, value: T[K]): this { this.obj[key] = value; return this; } build(): T { return this.obj; } } interface Book { title: string; pages: number; } const book = new Builder<Book>() .set('title', 'My Book') .set('pages', 100) .build(); console.log(book);
Attempts:
2 left
💡 Hint
Check how obj is initialized and if {} is assignable to T.
✗ Incorrect
The obj property is typed as T but initialized with {} which is not guaranteed to satisfy T. This causes a TypeError when assigning properties because obj is not properly initialized.
📝 Syntax
advanced2:00remaining
Which option correctly defines a generic builder method?
Which of these method signatures correctly defines a generic set method in a builder pattern class in TypeScript?
Attempts:
2 left
💡 Hint
Look for correct generic constraints and return type.
✗ Incorrect
Option A correctly uses a generic K constrained to keys of T, and the value type matches T[K]. It returns 'this' for chaining. Others have wrong types or return types.
🚀 Application
expert2:00remaining
How many properties does the built object have?
Consider this generic builder pattern code. How many properties does the final object have?
Typescript
class Builder<T> { private obj: Partial<T> = {}; set<K extends keyof T>(key: K, value: T[K]): this { this.obj[key] = value; return this; } build(): T { return this.obj as T; } } interface Config { host: string; port: number; secure: boolean; } const config = new Builder<Config>() .set('host', 'localhost') .set('port', 8080) .build(); console.log(Object.keys(config).length);
Attempts:
2 left
💡 Hint
Only properties set by set() exist in the object.
✗ Incorrect
The builder sets only 'host' and 'port'. The 'secure' property is missing, so Object.keys returns 2 keys.