Implement the Singleton Pattern

Medium Design Patterns & Final Challenges
Solve in Playground →

Problem Statement

Implement a `Database` class that strictly follows the Singleton design pattern. Ensure that multiple instantiations return the exact same object instance. Read an integer, set it as `db.value` on the first instance, create a second instance, and print its value to prove they share state.

Input Format

A single integer.

Output Format

Print the value from the second instance and a boolean checking identity.

Constraints

None

Sample Input

42

Sample Output

Value: 42
Same: True

Explanation

The Singleton pattern restricts the instantiation of a class to one single instance, sharing state globally.

Starter Code

class Database:
    _instance = None
    # Implement __new__ to enforce singleton pattern
    pass

if __name__ == '__main__':
    val = int(input().strip())
    # Instantiate db1, set value, instantiate db2, and verify
    pass

Limits

  • Time Limit: 1s
  • Memory Limit: 256MB

Embedded C Programming

Updated: March 15, 2026
Intermediate

Embedded systems rely on efficient low-level programming to interact directly with hardware. In this course, you will learn how to write practical Embedded C programs used in real microcontroller-based systems. Rather than focusing only on theory, this course follows a practice-driven approach. Each lesson includes hands-on coding exercises that simulate real firmware development tasks used