Design a Package Installer System with dependencies

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-02-13

Problem Statement

Design the Low-Level Design (LLD) for a Package Installer System, similar to how tools like npm, pip, apt, or Maven work.


The system manages software packages, their versions, and dependencies between them.


It should handle installing, uninstalling, and resolving dependencies while preventing cyclic or conflicting installs.


The focus is on object-oriented class design and dependency resolution logic — not on actual file downloads or OS-level execution.


🎯 Goal


Design a Package Installer System that can:


•Install packages with dependencies.
•Resolve dependency chains automatically.
•Detect and handle cyclic dependencies.
•Uninstall packages safely (without breaking others).
•Maintain a record of installed packages and their dependency graph.


✅ Functional Requirements


1. Package Management


•Each Package has:
•Name (e.g., "requests")
•Version (e.g., "2.28.1")
•List of dependencies (other packages with optional version constraints)


Example:

1 2Package A depends on B and C 3Package B depends on D

When installing A → system must install D, then B, then C, then A.


2. Installation


•When a user installs a package:
•The system should:
•Fetch all dependencies recursively.
•Install dependencies before the package itself.
•Skip packages that are already installed.
•Detect cyclic dependencies (e.g., A → B → C → A) and throw an error.


Example:

1 2install(A) 3A depends on B, C 4B depends on D 5=> Install D → B → C → A


3. Uninstallation


•When uninstalling a package:
•Remove it only if no other installed package depends on it.
•Otherwise, block the uninstall and warn the user.


Example:

1 2If A depends on B and user tries to remove B: 3→ Prevent removal since A still needs it.


4. Dependency Resolution


•Dependencies should be resolved in topological order (install dependencies first).
•Must maintain a directed acyclic graph (DAG) of dependencies.


5. State Management


•Keep track of:
•Installed packages
•Their dependencies
•Reference count (how many other packages depend on each package)


6. Commands (APIs)


•install(packageName)
•uninstall(packageName)
•listInstalledPackages()
•getDependencies(packageName)
•isInstalled(packageName)


⚙️ Non-Functional Requirements


•Deterministic: Install order should always be predictable.
•Efficient: Avoid redundant installations.
•Consistent: No broken dependencies after install/uninstall.
•Extensible: Easy to add versioning, caching, or remote registry later.


🧱 Key Classes to Design


ClassResponsibility


Package


Represents a software package (name, version, dependencies).


Dependency


Represents a link between two packages.


PackageRepository


Stores available packages and their dependency metadata.


InstalledPackageRegistry


Tracks currently installed packages and dependents.


DependencyResolver


Handles dependency graph traversal and topological sorting.


PackageInstaller


Orchestrates installation and uninstallation logic.


DependencyGraph


Represents the graph structure of package dependencies.


CycleDetector


Detects cyclic dependencies.


🧩 Suggested Class Structure

1 2Package 3 ├── String name 4 ├── String version 5 ├── List<String> dependencies 6 7DependencyGraph 8 ├── Map<String, List<String>> adjacencyList 9 ├── addEdge(String pkg, String dependency) 10 ├── List<String> topologicalSort() 11 ├── boolean hasCycle() 12 13PackageRepository 14 ├── Map<String, Package> availablePackages 15 ├── getPackage(String name) 16 17InstalledPackageRegistry 18 ├── Set<String> installedPackages 19 ├── Map<String, Set<String>> dependencyMap 20 ├── addPackage(String pkg) 21 ├── removePackage(String pkg) 22 ├── boolean isInstalled(String pkg) 23 24DependencyResolver 25 ├── List<String> resolveInstallOrder(String pkg) 26 ├── boolean detectCycle() 27 28PackageInstaller 29 ├── PackageRepository repo 30 ├── InstalledPackageRegistry registry 31 ├── DependencyResolver resolver 32 ├── void install(String pkg) 33 ├── void uninstall(String pkg) 34 ├── void listInstalled()


🔄 Example Flow


📥 Installation Example


Input

1 2Package A → depends on B, C 3Package B → depends on D 4Package C → no dependencies 5Package D → no dependencies


Command

1 2install(A)


Process


1.Resolver builds dependency graph: A → B, C; B → D
2.Perform topological sort → [D, B, C, A]
3.Install in order: D → B → C → A


Output

1 2Installing D 3Installing B 4Installing C 5Installing A


🗑️ Uninstallation Example


Command

1 2uninstall(B)

Output

1 2Cannot uninstall B — still required by A


🧠 Design Patterns


•Command Pattern: For install and uninstall operations.
•Observer Pattern: Notify system components when install/uninstall happens.
•Strategy Pattern: For different dependency resolution strategies (e.g., versioned).
•Singleton Pattern: For InstalledPackageRegistry.


⚡ Possible Extensions


•Add version resolution (semantic versioning).
•Add remote registry fetch for unavailable packages.
•Add rollback if installation fails mid-way.
•Add caching and persistence (e.g., local package DB).
•Add parallel installation for independent dependencies.


🧾 Example Scenario

1 2repo.addPackage(A, dependencies=[B, C]) 3repo.addPackage(B, dependencies=[D]) 4repo.addPackage(C, dependencies=[]) 5repo.addPackage(D, dependencies=[]) 6 7installer.install("A")


Output

1 2Installing D 3Installing B 4Installing C 5Installing A 6Installation complete.


✅ Final Summary:


Design a Package Installer System (LLD) that:


•Installs and uninstalls packages while resolving dependencies.
•Detects cycles and maintains consistent state.
•Uses a dependency graph for topological ordering.
•Follows modular, extensible OOP design.


Solution

Comments