Implement the Observer Pattern

Hard Design Patterns & Final Challenges
Solve in Playground →

Problem Statement

Create an `Observer` interface with `virtual void update(std::string state) = 0`. Create `Subject` that holds a `std::vector` and has `attach(Observer*)` and `setState(std::string)`. When state changes, call `update()` on all observers. Instantiate `Subject` and two observers, set state based on input, and print notifications.

Input Format

A single string representing the state.

Output Format

Print notifications for 'Obs 1' and 'Obs 2' on separate lines.

Constraints

None

Sample Input

Ready

Sample Output

Obs 1: Ready
Obs 2: Ready

Explanation

The Observer pattern provides a subscription mechanism, coupling one subject to many observers loosely.

Starter Code

#include <iostream>
#include <vector>
#include <string>

class Observer {
public:
    virtual void update(std::string state) = 0;
};

// Implement ConcreteObserver and Subject

int main() {
    // Setup pattern and trigger state change
    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