288. Design Feature Toggle Service
Design Feature Toggle Service
Design an in-memory feature toggle service that dynamically controls whether application features are available to users.

Feature Rules

  • Each feature has a unique name.
  • A newly added feature is disabled by default.
  • A feature may require a specific subscription, region, or both.
  • An empty subscription rule means that every subscription is allowed.
  • An empty region rule means that every region is allowed.
  • When both rules are present, the user must satisfy both rules.
  • A feature is available only when it exists, is enabled, and all its rules match the user.
  • Feature names, subscriptions, and regions are case-sensitive.

Class Initialization

FeatureToggleService()
Creates an empty feature toggle service.

Methods

Add a Feature

boolean addFeature(String featureName, String requiredSubscription, String requiredRegion)
Adds a disabled feature with the given rules.
  • featureName: The unique name of the feature. It contains characters [a-z] and hyphen -
  • requiredSubscription: The required subscription, such as "Premium". Use "" when there is no subscription restriction.
  • requiredRegion: The required region, such as "IN". Use "" when there is no region restriction.
  • Returns true when the feature is added.
  • Returns false when a feature with the same name already exists. The existing feature is not changed.

Enable a Feature

boolean enableFeature(String featureName)
Enables an existing feature.
  • Returns true when the feature exists, including when it was already enabled.
  • Returns false when the feature does not exist.

Disable a Feature

boolean disableFeature(String featureName)
Disables an existing feature.
  • Returns true when the feature exists, including when it was already disabled.
  • Returns false when the feature does not exist.

Remove a Feature

boolean removeFeature(String featureName)
Removes a feature and all its rules.
  • Returns true when the feature is removed.
  • Returns false when the feature does not exist.

Check Feature Availability

boolean isFeatureEnabled(String featureName, String userSubscription, String userRegion)
Checks whether the specified feature is available to a user.
  • userSubscription: The user's subscription. Use "" when the user has no subscription.
  • userRegion: The user's region. Use "" when the region is unavailable.
  • Returns true only when the feature exists, is enabled, and the user satisfies every configured rule.
  • Returns false otherwise.

Constraints

  • 1 ≤ featureName.length() ≤ 100
  • 0 ≤ requiredSubscription.length() ≤ 50
  • 0 ≤ requiredRegion.length() ≤ 20
  • 0 ≤ userSubscription.length() ≤ 50
  • 0 ≤ userRegion.length() ≤ 20
  • At most 100,000 features are stored at the same time.
  • All method parameters are non-null strings.

Examples

Example 1: Feature Available to Everyone

FeatureToggleService()
Output: A new feature toggle service is created.
addFeature(featureName = "skip-intro", requiredSubscription = "", requiredRegion = "")
Output: true
enableFeature(featureName = "skip-intro")
Output: true
isFeatureEnabled(featureName = "skip-intro", userSubscription = "Basic", userRegion = "BR")
Output: true
The enabled feature has no subscription or region restriction.

Example 2: Subscription and Region Rules

FeatureToggleService()
Output: A new feature toggle service is created.
addFeature(featureName = "ultra-hd", requiredSubscription = "Premium", requiredRegion = "IN")
Output: true
enableFeature(featureName = "ultra-hd")
Output: true
isFeatureEnabled(featureName = "ultra-hd", userSubscription = "Premium", userRegion = "IN")
Output: true
isFeatureEnabled(featureName = "ultra-hd", userSubscription = "Basic", userRegion = "IN")
Output: false
isFeatureEnabled(featureName = "ultra-hd", userSubscription = "Premium", userRegion = "US")
Output: false
The user must have the "Premium" subscription and belong to the "IN" region.

Example 3: Disable and Remove a Feature

FeatureToggleService()
Output: A new feature toggle service is created.
addFeature(featureName = "offline-downloads", requiredSubscription = "Standard", requiredRegion = "")
Output: true
enableFeature(featureName = "offline-downloads")
Output: true
disableFeature(featureName = "offline-downloads")
Output: true
isFeatureEnabled(featureName = "offline-downloads", userSubscription = "Standard", userRegion = "CA")
Output: false
removeFeature(featureName = "offline-downloads")
Output: true
enableFeature(featureName = "offline-downloads")
Output: false
A disabled feature is unavailable, and a removed feature no longer exists.


Please use Laptop/Desktop or any other large screen to add/edit code.