Problem: Build a Multi-Tenant Service Factory

hard
40 min
Create a factory that dynamically selects a service implementation based on tenant ID and service name.

Problem statement

You’re building a SaaS platform with multiple tenants. Each tenant can override specific services, such as Logger, Auth, or Mailer. Your system holds a global service registry structured like this:

const serviceRegistry = {
acme: {
Logger: class AcmeLogger {
log(msg) {
return `Acme log: ${msg}`;
}
}
},
default: {
Logger: class DefaultLogger {
log(msg) {
return `Default log: ${msg}`;
}
},
Auth: class DefaultAuth {
login(user) {
return `Default login for ${user}`;
}
}
}
};

Tenants may override zero, some, or all services. If a service is not defined for a tenant, it must fall back to the default implementation.

Goal

Implement createService(tenantId, serviceName) that returns the correct class instance—tenant-specific if defined, or default otherwise.

Constraints

  • Do not use if, switch, or ternaries to select or fall back between services.

  • Use object lookup and fallback chaining to resolve tenant-specific or default services.

  • Only instantiate the service inside the factory.

  • Consumers should not need to reference the "default" tenant explicitly—the factory must handle fallback logic automatically when a service is missing.

Sample output

The examples below illustrate what the output should look like:

const acmeLogger = createService('acme', 'Logger');
// Should use AcmeLogger (tenant override)
console.log(acmeLogger.log('Bootstrapped'));
/* Expected output:
Acme log: Bootstrapped */
const acmeAuth = createService('acme', 'Auth');
// Should fall back to DefaultAuth (not overridden by acme)
console.log(acmeAuth.login('alice'));
/* Expected output:
Default login for alice */
const unknownTenantLogger = createService('nonexistent', 'Logger');
// Should fall back to DefaultLogger (tenant doesn't exist)
console.log(unknownTenantLogger.log('Init'));
/* Expected output:
Default log: Init */
const defaultAuth = createService('default', 'Auth');
// Should still work when explicitly using default tenant
console.log(defaultAuth.login('bob'));
/* Expected output:
Default login for bob */

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

Problem: Build a Multi-Tenant Service Factory

hard
40 min
Create a factory that dynamically selects a service implementation based on tenant ID and service name.

Problem statement

You’re building a SaaS platform with multiple tenants. Each tenant can override specific services, such as Logger, Auth, or Mailer. Your system holds a global service registry structured like this:

const serviceRegistry = {
acme: {
Logger: class AcmeLogger {
log(msg) {
return `Acme log: ${msg}`;
}
}
},
default: {
Logger: class DefaultLogger {
log(msg) {
return `Default log: ${msg}`;
}
},
Auth: class DefaultAuth {
login(user) {
return `Default login for ${user}`;
}
}
}
};

Tenants may override zero, some, or all services. If a service is not defined for a tenant, it must fall back to the default implementation.

Goal

Implement createService(tenantId, serviceName) that returns the correct class instance—tenant-specific if defined, or default otherwise.

Constraints

  • Do not use if, switch, or ternaries to select or fall back between services.

  • Use object lookup and fallback chaining to resolve tenant-specific or default services.

  • Only instantiate the service inside the factory.

  • Consumers should not need to reference the "default" tenant explicitly—the factory must handle fallback logic automatically when a service is missing.

Sample output

The examples below illustrate what the output should look like:

const acmeLogger = createService('acme', 'Logger');
// Should use AcmeLogger (tenant override)
console.log(acmeLogger.log('Bootstrapped'));
/* Expected output:
Acme log: Bootstrapped */
const acmeAuth = createService('acme', 'Auth');
// Should fall back to DefaultAuth (not overridden by acme)
console.log(acmeAuth.login('alice'));
/* Expected output:
Default login for alice */
const unknownTenantLogger = createService('nonexistent', 'Logger');
// Should fall back to DefaultLogger (tenant doesn't exist)
console.log(unknownTenantLogger.log('Init'));
/* Expected output:
Default log: Init */
const defaultAuth = createService('default', 'Auth');
// Should still work when explicitly using default tenant
console.log(defaultAuth.login('bob'));
/* Expected output:
Default login for bob */

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

Node.js
// JavaScript boilerplate
const serviceRegistry = {
acme: {
Logger: class AcmeLogger {
log(msg) {
return `Acme log: ${msg}`;
}
}
},
default: {
Logger: class DefaultLogger {
log(msg) {
return `Default log: ${msg}`;
}
},
Auth: class DefaultAuth {
login(user) {
return `Default login for ${user}`;
}
}
}
};
// Implement this
function createService(tenantId, serviceName) {
// Return instance from tenant namespace or fall back to default
}
// Example usage
const acmeLogger = createService('acme', 'Logger');
// Should use AcmeLogger (tenant override)
console.log(acmeLogger.log('Bootstrapped'));
/* Expected output:
Acme log: Bootstrapped */
const acmeAuth = createService('acme', 'Auth');
// Should fall back to DefaultAuth (not overridden by acme)
console.log(acmeAuth.login('alice'));
/* Expected output:
Default login for alice */
const unknownTenantLogger = createService('nonexistent', 'Logger');
// Should fall back to DefaultLogger (tenant doesn't exist)
console.log(unknownTenantLogger.log('Init'));
/* Expected output:
Default log: Init */
const defaultAuth = createService('default', 'Auth');
// Should still work when explicitly using default tenant
console.log(defaultAuth.login('bob'));
/* Expected output:
Default login for bob */