Master technical and career interviews with structured answers—short definition, real examples, pitfalls, and how to answer in 60–90 seconds.
Answer: The Singleton Pattern can be implemented in a thread-safe manner, ensuring that in multi-threaded applications, only one instance is created even when multiple threads try to access it concurrently. What intervie…
The lock keyword is used to ensure that the singleton instance is created only once, even when multiple threads access the Instance property concurrently. This is important in multi-threaded applications where race condi…
Answer: Controls access to a resource by adding authorization checks before access is allowed. It can act as a gatekeeper for resources. What interviewers expect A clear definition tied to GoF Patterns in Gang of Four Pa…
Answer: By avoiding the creation of heavy objects until they're needed, proxies can improve the performance of applications, especially in cases of large datasets or expensive operations (like network calls or file loadi…
Answer: Once the real object is initialized, the proxy delegates the call to the real object. In our example, after the image is loaded by the proxy, it delegates the Display() method to the RealImage class. Benefits of…
The ProxyImage class also implements the IImage interface and controls access to the RealImage. It is responsible for lazy loading the real image only when needed (i.e., the first time Display() is called). public class…
Answer: In document editors, a document template (e.g., a "letter" prototype) can be cloned, and then the cloned document can be customized with specific content for each user. What interviewers expect A clear definition…
Answer: For game characters or systems that can exist in different states, like a character having different behaviors when idle, walking, running, or jumping. What interviewers expect A clear definition tied to GoF Patt…
Answer: If an object is very complex and its construction requires a lot of steps, the Prototype Pattern allows you to avoid duplicating these steps by cloning an existing object and modifying only the necessary parts. C…
Answer: When the Clone() method is called on an existing object (e.g., original), it returns a new instance of the same type (e.g., GameCharacter) with the same state (e.g., same Name and Health). Benefits of the Prototy…
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…
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Answer: The Singleton Pattern can be implemented in a thread-safe manner, ensuring that in multi-threaded applications, only one instance is created even when multiple threads try to access it concurrently.
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
only once, even when multiple threads access the Instance property
concurrently. This is important in multi-threaded applications where race
conditions could otherwise cause multiple instances to be created.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Answer: Controls access to a resource by adding authorization checks before access is allowed. It can act as a gatekeeper for resources.
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: By avoiding the creation of heavy objects until they're needed, proxies can improve the performance of applications, especially in cases of large datasets or expensive operations (like network calls or file loading).
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: Once the real object is initialized, the proxy delegates the call to the real object. In our example, after the image is loaded by the proxy, it delegates the Display() method to the RealImage class. Benefits of the Proxy 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
to the RealImage. It is responsible for lazy loading the real image only when needed
(i.e., the first time Display() is called).
public class ProxyImage : IImage
{
private readonly string _filename;
private RealImage _realImage;
public ProxyImage(string filename) => _filename = filename;
public void Display()
{
if (_realImage == null)
{
_realImage = new RealImage(_filename);
}
_realImage.Display();
}
}
Display() method is called for the first time. This delays the loading of the
image, making it more efficient if the Display() method is not called
frequently.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Answer: In document editors, a document template (e.g., a "letter" prototype) can be cloned, and then the cloned document can be customized with specific content for each user.
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: For game characters or systems that can exist in different states, like a character having different behaviors when idle, walking, running, or jumping.
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: If an object is very complex and its construction requires a lot of steps, the Prototype Pattern allows you to avoid duplicating these steps by cloning an existing object and modifying only the necessary parts. Considerations:
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 Clone() method is called on an existing object (e.g., original), it returns a new instance of the same type (e.g., GameCharacter) with the same state (e.g., same Name and Health). Benefits of the Prototype 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
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.