Master technical and career interviews with structured answers—short definition, real examples, pitfalls, and how to answer in 60–90 seconds.
Answer: In social media platforms, followers (observers) are notified when the user they follow (subject) posts new updates or content. What interviewers expect A clear definition tied to GoF Patterns in Gang of Four Pat…
Answer: Care should be taken to avoid circular dependencies, where observers depend on each other in a way that could create an infinite loop or inconsistent states. Real-Time Use Case Examples: What interviewers expect…
Answer: All observers receive the update from the subject automatically, ensuring that they all stay in sync with the subject’s state. What interviewers expect A clear definition tied to GoF Patterns in Gang of Four Patt…
Answer: When the publisher publishes new news via the Notify() method, each observer’s Update() method is called, and the news is sent to all registered subscribers. Benefits of the Observer Pattern: What interviewers ex…
The NewsPublisher class is the concrete implementation of the subject. It maintains a list of observers and provides methods to subscribe, unsubscribe, and notify them when a new news article is available. public class N…
Answer: In web applications or forms, the Memento Pattern can be used to save the state of form inputs at various stages. This allows users to undo their changes or restore the form to a previous valid state. What interv…
You can extend the pattern to support multiple levels of undo by adding more sophisticated memento management (e.g., limiting the number of mementos kept in memory or implementing a more efficient undo/redo system). Real…
Answer: The Caretaker is responsible for managing the saved states (mementos). It can undo changes by restoring the TextEditor to its previous state stored in the mementos stack. What interviewers expect A clear definiti…
While the mediator reduces direct dependencies between colleagues, it can also create a dependency on the mediator itself. Over-reliance on the mediator can lead to issues if the mediator needs to change. Visual Diagram:…
In a graphical user interface (GUI) system, the Mediator Pattern can be used to manage interactions between various components like buttons, text fields, and labels. For example, clicking a button might update a text fie…
Answer: Instead of having complex direct interactions between objects (users in this case), the mediator simplifies the process, as objects only need to communicate with the mediator. What interviewers expect A clear def…
Answer: Users don’t need to know the identities of other users or how to reach them. The mediator centralizes communication, and the users only rely on the mediator to send and receive messages. Benefits of the Mediator…
The ChatMediator class is the concrete mediator that implements IChatMediator. It manages a list of users and is responsible for broadcasting messages to all registered users, except the one who sent the message. The med…
For very large collections, optimizing the iterator to handle bulk operations efficiently (e.g., lazy loading or batching) can improve performance. Visual Diagram: Follow: +---------------------------+ | IIterator<T&g…
When querying a database, the results often come back in the form of a collection (like a list of rows). The Iterator Pattern is used to iterate over these rows to access the data, rather than exposing the internal struc…
The Iterator Pattern allows the collection’s internal structure to be hidden from the client. The client interacts only with the iterator, which means that changes to the underlying collection (e.g., changing it from a l…
The IAggregate<T> interface defines a method CreateIterator() that returns an iterator instance. Any class that represents a collection should implement this interface to provide an iterator. public interface IAggr…
Answer: The Interpreter Pattern can be extended to support more complex grammars. For example, adding new operations like subtraction or division can be easily done by introducing new non-terminal expressions (e.g., Subt…
The Interpreter Pattern is commonly used in building parsers for domain-specific languages (DSLs) or simple programming languages. Each statement or expression in the language can be represented as an object, and the int…
Answer: New operations (e.g., subtraction, multiplication, etc.) can be added easily by creating new non-terminal expression classes (e.g., Subtract, Multiply). This makes the pattern highly extendable. What interviewers…
Answer: The Number class is a terminal expression that holds a single value. When the Interpret method is called, it returns the value of the number. Terminal expressions represent the simplest elements in the language o…
The Flyweight Pattern could be extended to support composite objects where each flyweight can contain references to other flyweights. For example, a complex character (e.g., with styling information) could consist of sev…
Answer: GUI frameworks that display multiple similar graphical elements (e.g., buttons, labels, icons) can use the Flyweight Pattern to reuse common elements while only storing the unique aspects (such as position, text,…
When the program requests a Character for each letter in the string "Hello World", the CharacterFactory checks if the character already exists. If it does, the existing object is reused; otherwise, a new Character object…
Answer: The factory manages the extrinsic state (e.g., the position where the character is rendered) separately. It ensures that intrinsic state (the symbol) is shared between all instances, preventing the creation of du…
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Answer: In social media platforms, followers (observers) are notified when the user they follow (subject) posts new updates or content.
In a production Gang of Four Patterns application, teams apply this when handling user-facing features or integration boundaries. For example, you might use it during a sprint where reliability and observability matter—logging metrics, validating edge cases, and documenting the decision in an ADR so future developers understand why the approach was chosen.
Tip: Practice aloud on Toolliyo mock interview or the Interview Q&A section before your real interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Answer: Care should be taken to avoid circular dependencies, where observers depend on each other in a way that could create an infinite loop or inconsistent states. Real-Time Use Case Examples:
In a production Gang of Four Patterns application, teams apply this when handling user-facing features or integration boundaries. For example, you might use it during a sprint where reliability and observability matter—logging metrics, validating edge cases, and documenting the decision in an ADR so future developers understand why the approach was chosen.
Tip: Practice aloud on Toolliyo mock interview or the Interview Q&A section before your real interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Answer: All observers receive the update from the subject automatically, ensuring that they all stay in sync with the subject’s state.
In a production Gang of Four Patterns application, teams apply this when handling user-facing features or integration boundaries. For example, you might use it during a sprint where reliability and observability matter—logging metrics, validating edge cases, and documenting the decision in an ADR so future developers understand why the approach was chosen.
Tip: Practice aloud on Toolliyo mock interview or the Interview Q&A section before your real interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Answer: When the publisher publishes new news via the Notify() method, each observer’s Update() method is called, and the news is sent to all registered subscribers. Benefits of the Observer Pattern:
In a production Gang of Four Patterns application, teams apply this when handling user-facing features or integration boundaries. For example, you might use it during a sprint where reliability and observability matter—logging metrics, validating edge cases, and documenting the decision in an ADR so future developers understand why the approach was chosen.
Tip: Practice aloud on Toolliyo mock interview or the Interview Q&A section before your real interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
maintains a list of observers and provides methods to subscribe, unsubscribe, and
notify them when a new news article is available.
public class NewsPublisher : INewsPublisher
{
private readonly List<IObserver> _observers = new
List<IObserver>();
public void Subscribe(IObserver observer) =>
_observers.Add(observer);
public void Unsubscribe(IObserver observer) =>
_observers.Remove(observer);
public void Notify(string news)
{
foreach (var observer in _observers)
{
observer.Update(news);
}
}
}Gang of Four Patterns Design Patterns in C# · GoF Patterns
Answer: In web applications or forms, the Memento Pattern can be used to save the state of form inputs at various stages. This allows users to undo their changes or restore the form to a previous valid state.
In a production Gang of Four Patterns application, teams apply this when handling user-facing features or integration boundaries. For example, you might use it during a sprint where reliability and observability matter—logging metrics, validating edge cases, and documenting the decision in an ADR so future developers understand why the approach was chosen.
Tip: Practice aloud on Toolliyo mock interview or the Interview Q&A section before your real interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
sophisticated memento management (e.g., limiting the number of mementos
kept in memory or implementing a more efficient undo/redo system).
Real-Time Use Case Examples:
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Answer: The Caretaker is responsible for managing the saved states (mementos). It can undo changes by restoring the TextEditor to its previous state stored in the mementos stack.
In a production Gang of Four Patterns application, teams apply this when handling user-facing features or integration boundaries. For example, you might use it during a sprint where reliability and observability matter—logging metrics, validating edge cases, and documenting the decision in an ADR so future developers understand why the approach was chosen.
Tip: Practice aloud on Toolliyo mock interview or the Interview Q&A section before your real interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
also create a dependency on the mediator itself. Over-reliance on the
mediator can lead to issues if the mediator needs to change.
Visual Diagram:
+----------------------+
| IChatMediator |
| (Mediator Interface) |
+----------------------+
+------------------------------------+
| |
+------------------+ +------------------+
| ChatMediator | | User |
| (Concrete Mediator) | (Colleague) |
+------------------+ +------------------+
| |
+-------------------+ +------------------+
| RegisterUser(User)| | Send(string) |
| SendMessage(...) | | Receive(string) |
+-------------------+ +------------------+
Follow:
Conclusion:
The Mediator Pattern is an excellent solution for managing complex interactions between
objects in a system, particularly when those objects don’t need to know about each other
directly. It reduces dependencies, simplifies communication, and centralizes control, making
it easier to manage interactions. However, it should be used judiciously, as a poorly
implemented mediator can become a bottleneck or a single point of failure in the system.
Memento Pattern: Real-Time Example - Undo Feature in a Text Editor
Definition:
The Memento Pattern is used to capture and externalize an object's internal state without
violating encapsulation. This allows the object to be restored to this state later. It’s commonly
used in situations where an object's state changes over time and you may need to revert to
previous states, such as an undo feature.
Use Case:
The Memento Pattern is widely used in scenarios where you want to implement an undo or
restore functionality, such as in a text editor. In this case, the pattern allows the editor to
save versions of the text and restore them when the user requests an undo.
Code Breakdown:
Gang of Four Patterns Design Patterns in C# · GoF Patterns
to manage interactions between various components like buttons, text fields,
and labels. For example, clicking a button might update a text field, and the
mediator ensures that these updates are propagated correctly.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Answer: Instead of having complex direct interactions between objects (users in this case), the mediator simplifies the process, as objects only need to communicate with the mediator.
In a production Gang of Four Patterns application, teams apply this when handling user-facing features or integration boundaries. For example, you might use it during a sprint where reliability and observability matter—logging metrics, validating edge cases, and documenting the decision in an ADR so future developers understand why the approach was chosen.
Tip: Practice aloud on Toolliyo mock interview or the Interview Q&A section before your real interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Answer: Users don’t need to know the identities of other users or how to reach them. The mediator centralizes communication, and the users only rely on the mediator to send and receive messages. Benefits of the Mediator Pattern:
In a production Gang of Four Patterns application, teams apply this when handling user-facing features or integration boundaries. For example, you might use it during a sprint where reliability and observability matter—logging metrics, validating edge cases, and documenting the decision in an ADR so future developers understand why the approach was chosen.
Tip: Practice aloud on Toolliyo mock interview or the Interview Q&A section before your real interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
IChatMediator. It manages a list of users and is responsible for
broadcasting messages to all registered users, except the one who sent the
message.
to know about each other's existence.
public class ChatMediator : IChatMediator
{
private readonly List<User> _users = new List<User>();
public void RegisterUser(User user) => _users.Add(user);
public void SendMessage(string message, User user)
{
foreach (var u in _users)
{
// Message should not be sent to the user who sent it
if (u != user)
{
u.Receive(message);
}
}
}
}Gang of Four Patterns Design Patterns in C# · GoF Patterns
efficiently (e.g., lazy loading or batching) can improve performance.
Visual Diagram:
Follow:
+---------------------------+
| IIterator<T> |
| (Iterator Interface) |
+---------------------------+
+---------------------------+
| |
+-----------------+ +------------------+
| ProductIterator| | ProductCollection|
| (Concrete Iterator) | (Concrete Aggregate)|
+-----------------+ +------------------+
| |
+--------------+ +--------------+
| HasNext() | | Add() |
| Next() | | Count |
| | | CreateIterator() |
+--------------+ +--------------+
Conclusion:
The Iterator Pattern is a powerful design pattern for accessing elements of a collection
sequentially, encapsulating the iteration logic in a separate object. This allows for greater
flexibility and maintainability by decoupling the collection's internal representation from the
client code.
Mediator Pattern: Real-Time Example - Chat Application
Definition:
The Mediator Pattern defines an object that encapsulates how a set of objects interact. It
promotes loose coupling by preventing objects from referring to each other explicitly,
allowing them to communicate indirectly through the mediator. This pattern is useful when
you need to manage complex interactions between multiple objects, without them needing to
know about each other.
Use Case:
Follow:
A chat application is a perfect example of where the Mediator Pattern can be applied. In a
chat app, users (colleagues) need to communicate, but rather than each user being directly
aware of the others, a mediator handles all the communication between users.
Code Explanation:
Gang of Four Patterns Design Patterns in C# · GoF Patterns
collection (like a list of rows). The Iterator Pattern is used to iterate over
these rows to access the data, rather than exposing the internal structure of
how the data is retrieved from the database.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
from the client. The client interacts only with the iterator, which means that
changes to the underlying collection (e.g., changing it from a list to a linked
list) do not affect the client code.Gang of Four Patterns Design Patterns in C# · GoF Patterns
returns an iterator instance. Any class that represents a collection should
implement this interface to provide an iterator.
public interface IAggregate<T>
{
IIterator<T> CreateIterator();
}Gang of Four Patterns Design Patterns in C# · GoF Patterns
Answer: The Interpreter Pattern can be extended to support more complex grammars. For example, adding new operations like subtraction or division can be easily done by introducing new non-terminal expressions (e.g., Subtract, Divide).
In a production Gang of Four Patterns application, teams apply this when handling user-facing features or integration boundaries. For example, you might use it during a sprint where reliability and observability matter—logging metrics, validating edge cases, and documenting the decision in an ADR so future developers understand why the approach was chosen.
Tip: Practice aloud on Toolliyo mock interview or the Interview Q&A section before your real interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
domain-specific languages (DSLs) or simple programming languages. Each
statement or expression in the language can be represented as an object,
and the interpreter evaluates these statements to execute the program.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Answer: New operations (e.g., subtraction, multiplication, etc.) can be added easily by creating new non-terminal expression classes (e.g., Subtract, Multiply). This makes the pattern highly extendable.
In a production Gang of Four Patterns application, teams apply this when handling user-facing features or integration boundaries. For example, you might use it during a sprint where reliability and observability matter—logging metrics, validating edge cases, and documenting the decision in an ADR so future developers understand why the approach was chosen.
Tip: Practice aloud on Toolliyo mock interview or the Interview Q&A section before your real interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Answer: The Number class is a terminal expression that holds a single value. When the Interpret method is called, it returns the value of the number. Terminal expressions represent the simplest elements in the language or grammar.
In a production Gang of Four Patterns application, teams apply this when handling user-facing features or integration boundaries. For example, you might use it during a sprint where reliability and observability matter—logging metrics, validating edge cases, and documenting the decision in an ADR so future developers understand why the approach was chosen.
Tip: Practice aloud on Toolliyo mock interview or the Interview Q&A section before your real interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
where each flyweight can contain references to other flyweights. For example,
a complex character (e.g., with styling information) could consist of several
flyweight components (like the base character, font style, size, etc.).
Visual Diagram:
+---------------------------+
| CharacterFactory |
| (Flyweight Factory) |
+---------------------------+
+---------------------------------------------+
| |
+------------------+
+------------------+
| Character | | Character
| <--- Flyweight Objects
| (Intrinsic State)| | (Intrinsic
State)|
+------------------+
+------------------+
| |
| * Shared across all objects |
Follow:
+--------------------------------------------------->+
(Position, Size, Text displayed are external/unique)
(Memory saved by sharing the intrinsic state)
Conclusion:
The Flyweight Pattern provides a powerful way to manage large numbers of similar objects
efficiently by sharing common state and minimizing memory usage. It’s particularly beneficial
in scenarios like text rendering, game graphics, or large-scale simulations where creating
numerous identical objects would be costly in terms of memory and performance. By
applying this pattern, you can significantly reduce the memory footprint and improve the
performance of your application while maintaining flexibility in managing the objects' unique
properties.
Interpreter Pattern: Real-Time Example - Parsing and Evaluating
Mathematical Expressions
Definition:
The Interpreter Pattern defines a representation for a grammar along with an interpreter to
interpret sentences in that grammar. It is used to evaluate expressions or interpret complex
languages by breaking them down into simpler components that can be recursively
evaluated.
Use Case:
A typical use case for the Interpreter Pattern is parsing and evaluating mathematical
expressions, like addition, subtraction, multiplication, or division. It allows for flexible and
dynamic evaluation of complex expressions.
Code Explanation:
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Answer: GUI frameworks that display multiple similar graphical elements (e.g., buttons, labels, icons) can use the Flyweight Pattern to reuse common elements while only storing the unique aspects (such as position, text, or color).
In a production Gang of Four Patterns application, teams apply this when handling user-facing features or integration boundaries. For example, you might use it during a sprint where reliability and observability matter—logging metrics, validating edge cases, and documenting the decision in an ADR so future developers understand why the approach was chosen.
Tip: Practice aloud on Toolliyo mock interview or the Interview Q&A section before your real interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
World", the CharacterFactory checks if the character already exists. If it
does, the existing object is reused; otherwise, a new Character object is
created.
common objects.
Key Benefits of the Flyweight Pattern:
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Answer: The factory manages the extrinsic state (e.g., the position where the character is rendered) separately. It ensures that intrinsic state (the symbol) is shared between all instances, preventing the creation of duplicate objects.
In a production Gang of Four Patterns application, teams apply this when handling user-facing features or integration boundaries. For example, you might use it during a sprint where reliability and observability matter—logging metrics, validating edge cases, and documenting the decision in an ADR so future developers understand why the approach was chosen.
Tip: Practice aloud on Toolliyo mock interview or the Interview Q&A section before your real interview.