Problem: Create a Feature-Flagged Analytics Service

Hard
40 min
Return either a real analytics service or a stub based on a feature flag, using only factory logic.

Problem statement

You’re rolling out a new analytics module, but it’s not yet available to all users. It’s gated behind a flags.analyticsEnabled feature flag. The team needs a factory that returns either:

  • A real AnalyticsService when the flag is on.

  • A stub that does nothing when the flag is off.

Both methods must be exposed: .track() and .report(). This ensures the rest of the codebase can safely call the service without knowing which version was returned.

Goal

Implement createAnalyticsService(flags) that returns either a real or stub service, with matching method signatures.

Constraints

  • Do not use conditionals (ifswitch, ternaries) to select which class to instantiate.

  • The factory must return an object with .track() and .report() either way.

  • Stub and real classes must only be instantiated within the factory.

Sample output

The examples below illustrate what the output should look like:

const flags1 = { analyticsEnabled: true };
const analytics1 = createAnalyticsService(flags1);
analytics1.track('user.signup');
analytics1.report();
/* Expected output:
AnalyticsService initialized
Tracking event: user.signup
Generating report */
const flags2 = { analyticsEnabled: false };
const analytics2 = createAnalyticsService(flags2);
analytics2.track('user.login');
analytics2.report();
/* No output expected */

Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.

Problem: Create a Feature-Flagged Analytics Service

Hard
40 min
Return either a real analytics service or a stub based on a feature flag, using only factory logic.

Problem statement

You’re rolling out a new analytics module, but it’s not yet available to all users. It’s gated behind a flags.analyticsEnabled feature flag. The team needs a factory that returns either:

  • A real AnalyticsService when the flag is on.

  • A stub that does nothing when the flag is off.

Both methods must be exposed: .track() and .report(). This ensures the rest of the codebase can safely call the service without knowing which version was returned.

Goal

Implement createAnalyticsService(flags) that returns either a real or stub service, with matching method signatures.

Constraints

  • Do not use conditionals (ifswitch, ternaries) to select which class to instantiate.

  • The factory must return an object with .track() and .report() either way.

  • Stub and real classes must only be instantiated within the factory.

Sample output

The examples below illustrate what the output should look like:

const flags1 = { analyticsEnabled: true };
const analytics1 = createAnalyticsService(flags1);
analytics1.track('user.signup');
analytics1.report();
/* Expected output:
AnalyticsService initialized
Tracking event: user.signup
Generating report */
const flags2 = { analyticsEnabled: false };
const analytics2 = createAnalyticsService(flags2);
analytics2.track('user.login');
analytics2.report();
/* No output expected */

Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.

Node.js
class AnalyticsService {
constructor() {
console.log('AnalyticsService initialized');
}
track(event) {
console.log('Tracking event:', event);
}
report() {
console.log('Generating report');
}
}
// Implement this
function createAnalyticsService(flags) {
// Return real or stub service based on flags.analyticsEnabled
}
// Example usage
const flags1 = { analyticsEnabled: true };
const analytics1 = createAnalyticsService(flags1);
analytics1.track('user.signup');
analytics1.report();
/* Expected output:
AnalyticsService initialized
Tracking event: user.signup
Generating report */
console.log('---');
const flags2 = { analyticsEnabled: false };
const analytics2 = createAnalyticsService(flags2);
analytics2.track('user.login');
analytics2.report();
/* No output expected */