Implement the Strategy Pattern

Hard Design Patterns & Final Challenges
Solve in Playground →

Problem Statement

Create a `Strategy` interface with `virtual int execute(int a, int b) = 0`. Implement `AddStrategy` and `MultiplyStrategy`. Create a `Context` class that takes a `Strategy*` and executes it. Read a string (‘add’ or ‘mul’) followed by two integers. Apply the correct strategy and print ‘Result: [val]’.

Input Format

String ('add' or 'mul') and two integers.

Output Format

Print the result of the strategy execution.

Constraints

None

Sample Input

add 5 3

Sample Output

Result: 8

Explanation

The Strategy pattern allows an algorithm's behavior to be selected at runtime by encapsulating interchangeable logic inside class objects.

Starter Code

#include <iostream>
#include <string>

class Strategy {
public:
    virtual int execute(int a, int b) = 0;
};

// Implement Concrete Strategies and Context

int main() {
    // Setup context, inject strategy, execute
    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