Problem: Enforce a Required Build Sequence in a Signup Form
Problem statement
You’re designing a secure sign-up flow for a backend service. Each sign-up form must include a username and password before proceeding to optional steps, such as email, profile image, or phone number.
You’ve been asked to implement a SignupFormBuilder that enforces this order of construction. Developers must call .withUsername() and .withPassword() first—in any order—but no other method (including .build()) should be allowed until both are set.
Goal
Implement a SignupFormBuilder with the following methods:
.withUsername(username: string): Sets the username (Required)..withPassword(password: string): Sets the password (Required)..withEmail(email: string): Adds an optional email..withPhone(phone: string): Adds an optional phone number..withAvatar(url: string): Adds an optional avatar URL..build(): Returns an object with shape:{ username: string, password: string, email?: string, phone?: string, avatar?: string }.
Calling any optional method or .build() before both required fields are set must throw an error.
Constraints
Enforce the required sequence via internal state tracking—not static typing.
Do not allow
.build()or optional setters to proceed until both required fields are set.Avoid returning partial objects or silently accepting incomplete builds.
Sample output
The examples below illustrate what the output should look like:
// Validconst form1 = new SignupFormBuilder().withUsername('alice').withPassword('secure123').withEmail('alice@example.com').withAvatar('https://cdn/avatar.png').build();console.log(form1);/* Expected output:{ username: 'alice',password: 'secure123',email: 'alice@example.com',avatar: 'https://cdn/avatar.png' } */// Invalid: build too earlytry {new SignupFormBuilder().withUsername('bob').build();} catch (err) {console.log('Error:', err.message);}/* Expected output:Error: Cannot call build before setting both username and password */// Invalid: optional field before requiredtry {new SignupFormBuilder().withEmail('x@x.com').withUsername('x').withPassword('y').build();} catch (err) {console.log('Error:', err.message);}/* Expected output:Error: Cannot call withEmail before setting both username and password */
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.
Problem: Enforce a Required Build Sequence in a Signup Form
Problem statement
You’re designing a secure sign-up flow for a backend service. Each sign-up form must include a username and password before proceeding to optional steps, such as email, profile image, or phone number.
You’ve been asked to implement a SignupFormBuilder that enforces this order of construction. Developers must call .withUsername() and .withPassword() first—in any order—but no other method (including .build()) should be allowed until both are set.
Goal
Implement a SignupFormBuilder with the following methods:
.withUsername(username: string): Sets the username (Required)..withPassword(password: string): Sets the password (Required)..withEmail(email: string): Adds an optional email..withPhone(phone: string): Adds an optional phone number..withAvatar(url: string): Adds an optional avatar URL..build(): Returns an object with shape:{ username: string, password: string, email?: string, phone?: string, avatar?: string }.
Calling any optional method or .build() before both required fields are set must throw an error.
Constraints
Enforce the required sequence via internal state tracking—not static typing.
Do not allow
.build()or optional setters to proceed until both required fields are set.Avoid returning partial objects or silently accepting incomplete builds.
Sample output
The examples below illustrate what the output should look like:
// Validconst form1 = new SignupFormBuilder().withUsername('alice').withPassword('secure123').withEmail('alice@example.com').withAvatar('https://cdn/avatar.png').build();console.log(form1);/* Expected output:{ username: 'alice',password: 'secure123',email: 'alice@example.com',avatar: 'https://cdn/avatar.png' } */// Invalid: build too earlytry {new SignupFormBuilder().withUsername('bob').build();} catch (err) {console.log('Error:', err.message);}/* Expected output:Error: Cannot call build before setting both username and password */// Invalid: optional field before requiredtry {new SignupFormBuilder().withEmail('x@x.com').withUsername('x').withPassword('y').build();} catch (err) {console.log('Error:', err.message);}/* Expected output:Error: Cannot call withEmail before setting both username and password */
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.