Implement the Singleton Pattern

Medium Design Patterns & Final Challenges
Solve in Playground →

Problem Statement

Implement a `Database` class in C++ that strictly follows the Singleton design pattern using a static instance pointer and a private constructor. Ensure that multiple instantiations return the exact same object instance. Read an integer, set it as `value` on the first instance reference, get a second instance reference, and print its value to prove they share state.

Input Format

A single integer.

Output Format

Print the value retrieved from the second instance reference.

Constraints

None

Sample Input

42

Sample Output

Value: 42

Explanation

The Singleton pattern restricts the instantiation of a class to one single instance, sharing state globally. Returning a static local instance by reference is the modern thread-safe way in C++.

Starter Code

#include <iostream>

class Database {
    // Implement private constructor and static getInstance method
};

int main() {
    // Retrieve instance, set value, retrieve again, and print
    return 0;
}

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