Master technical and career interviews with structured answers—short definition, real examples, pitfalls, and how to answer in 60–90 seconds.
Short answer: The Composite Pattern can also be used in organizational structures, where departments (composites) contain teams or employees (individuals), and both can be treated as "components" with a common…
Short answer: The recursive structure makes it easy to manage hierarchical data. For example, when displaying the contents of a directory, you don’t need to worry about whether the child is a file or a subdirectory. Say…
Short answer: The File class represents the leaf in the tree structure. It is an individual component (a file) and implements the ShowInfo() method to display its details. public class File : IFileSystemComponent Example…
Short answer: You can combine multiple commands into a composite command. For example, if the user performs a series of actions (like adding text, changing fonts, and changing colors), you can encapsulate all of those ac…
Short answer: The Command Pattern makes it easy to add new commands without modifying existing code. For example, you can add new commands like RemoveTextCommand or ChangeFontCommand by simply implementing the ICommand i…
Short answer: The TextEditor maintains a stack of executed commands. When the Undo() method is called, it pops the most recent command from the stack and calls its Undo() method, which reverts the action performed by the…
Short answer: The AddTextCommand is a concrete implementation of the ICommand interface. It encapsulates the request to add text to the document. The Execute() method adds the specified text to the document, and the Undo…
Short answer: .LogMessage(...)). This continues until a handler processes the message or the chain is exhausted. Key Benefits of the Chain of Responsibility Pattern: Real-world example (ShopNest) Use a GoF pattern in Sho…
Short answer: The InfoLogger and ErrorLogger are concrete handlers that implement the Logger class. Explain a bit more Each handler checks if it can handle a particular log level (e.g., Info or Error). If it can, it proc…
Short answer: The Builder Pattern allows you to easily create different variants of an object (e.g., different types of pizzas) without changing the client code. You can add new builders for other pizza types without mod…
Short answer: The IPizzaBuilder interface defines the steps for constructing a pizza. It includes methods for setting the dough, sauce, and adding toppings, as well as a Build() method to return the fully constructed piz…
Short answer: The separation of concerns means that any changes to the rendering logic (e.g., upgrading the drawing API) do not affect the shape logic, and vice versa. This results in less risk of introducing bugs when m…
Short answer: The IDrawingAPI interface defines the drawing method (DrawCircle in this case). Different concrete implementations of this interface will represent various drawing styles or technologies. Example code publi…
Short answer: The pattern ensures that the UI components are consistent with the platform's look and feel, as each factory provides platform-specific products. Explain a bit more Improvement Suggestions: Extendability: Y…
Short answer: The WindowsUIFactory and MacUIFactory are concrete implementations of the IUIFactory interface. Each factory creates platform-specific objects like buttons and checkboxes. public class WindowsUIFactory : IU…
Short answer: ction and then set the next state, allowing the traffic light to cycle through its states (Red → Green → Yellow → Red). Benefits of the State Pattern: Real-world example (ShopNest) Use a GoF pattern in Shop…
Short answer: dditional checks like authentication or caching. Conclusion: The Proxy Pattern is an excellent design pattern to control access to expensive or sensitive objects. Real-world example (ShopNest) Use a GoF pat…
Short answer: ccess to the real object. For example, it could authenticate users before llowing access to a sensitive resource. Types of Proxy: Say this in the interview Define — one clear sentence (the short answer abov…
Short answer: nimals) can be cloned from a prototype, allowing for rapid creation of multiple instances with different states. Explain a bit more Deep Cloning Example: If you need to perform deep cloning, where not just…
Short answer: And implements the Update() method to receive news updates from the publisher. Explain a bit more public class NewsSubscriber : IObserver { private readonly string _name; public NewsSubscriber(string name)…
Short answer: re passed along the pipeline in a controlled and coordinated manner. Considerations and Drawbacks: Real-world example (ShopNest) Use a GoF pattern in ShopNest only when it removes duplication or makes a cha…
Short answer: s an expression, and the pattern allows for flexible and extensible parsing rules. Improvement Suggestions: Real-world example (ShopNest) Use a GoF pattern in ShopNest only when it removes duplication or ma…
Short answer: ny component and let the pattern take care of the rest. Real-Time Use Case Example: Say this in the interview Define — one clear sentence (the short answer above). Example — relate it to a project like Shop…
Short answer: By adding new visitors (e.g., another visitor for tax calculation or shipping fees), you can introduce new operations on the same set of elements without modifying their classes. This is the key benefit of…
Short answer: The concrete visitor ShoppingCart implements the IShoppingCartVisitor interface. It performs specific operations on each element, like calculating the total cost with any applicable discounts. For instance,…
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: The Composite Pattern can also be used in organizational structures, where departments (composites) contain teams or employees (individuals), and both can be treated as "components" with a common interface for operations like calculating total salary or generating reports. Improvement Suggestions:
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: The recursive structure makes it easy to manage hierarchical data. For example, when displaying the contents of a directory, you don’t need to worry about whether the child is a file or a subdirectory.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: The File class represents the leaf in the tree structure. It is an individual component (a file) and implements the ShowInfo() method to display its details. public class File : IFileSystemComponent
{
private string _name;
public File(string name) => _name = name;
public void ShowInfo() => Console.WriteLine($"File: {_name}");
}
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: You can combine multiple commands into a composite command. For example, if the user performs a series of actions (like adding text, changing fonts, and changing colors), you can encapsulate all of those actions into a single composite command that can be undone in one step. Real-Time Use Case
The Command Pattern is often used in: Text Editors: For implementing undo/redo functionality and user interactions. Transaction Systems: Where each action can be encapsulated as a command, and the entire system can be undone or redone. GUI Frameworks: Buttons, sliders, and menu items can be mapped to commands, allowing for consistent handling of actions across the UI. Gaming Applications: Where player actions (e.g., moving, shooting) can be encapsulated as commands and undone when necessary. Visual Diagram: Here's a simple visual diagram to understand the Command Pattern: +-----------------+ +---------------------+ +------------------+ | TextEditor | ---> | AddTextCommand | ---> | Document | | (Invoker) | | (Concrete Command) | | (Receiver) | +-----------------+ +---------------------+ +------------------+ | | +-----------+ +-------------------+ | Undo | | AddText / RemoveText | +-----------+ +-------------------+ The Command Pattern provides a flexible and scalable way to handle requests in object-oriented systems, especially when you need to manage complex workflows, implement undo/redo functionality, or decouple senders from receivers. Composite Pattern: Real-Time Example - Building a File System Scenario: The Composite Pattern is used when you need to treat individual objects and compositions of objects uniformly. This is particularly useful when you have a hierarchical structure, like a file system, where files and directories can be treated in a similar manner. In a file system: Files are the individual objects (leaves). Directories are composite objects that can contain files or other directories (children). This pattern helps to simplify the management of hierarchical structures, making it easier to handle both individual items and collections of items in a unified way. Code Explanation:
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: The Command Pattern makes it easy to add new commands without modifying existing code. For example, you can add new commands like RemoveTextCommand or ChangeFontCommand by simply implementing the ICommand interface.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: The TextEditor maintains a stack of executed commands. When the Undo() method is called, it pops the most recent command from the stack and calls its Undo() method, which reverts the action performed by the command (removes the last added text). Key Benefits of the Command Pattern:
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: The AddTextCommand is a concrete implementation of the ICommand interface. It encapsulates the request to add text to the document. The Execute() method adds the specified text to the document, and the Undo() method removes that text. public class AddTextCommand : ICommand
{
private readonly Document _document;
private readonly string _text;
public AddTextCommand(Document document, string text)
{
_document = document;
_text = text;
}
public void Execute() => _document.AddText(_text);
public void Undo() => _document.RemoveText(_text);
}
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: .LogMessage(...)). This continues until a handler processes the message or the chain is exhausted. Key Benefits of the Chain of Responsibility Pattern:
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: The InfoLogger and ErrorLogger are concrete handlers that implement the Logger class.
Each handler checks if it can handle a particular log level (e.g., Info or Error). If it can, it processes the log; otherwise, it passes it along to the next handler in the chain. public class InfoLogger : Logger { protected override bool CanHandle(LogLevel level) => level == LogLevel.Info; protected override void Handle(string message) => Console.WriteLine($"Info: {message}"); }
public class ErrorLogger : Logger
{ protected override bool CanHandle(LogLevel level) => level == LogLevel.Error; protected override void Handle(string message) => Console.WriteLine($"Error: {message}"); }
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: The Builder Pattern allows you to easily create different variants of an object (e.g., different types of pizzas) without changing the client code. You can add new builders for other pizza types without modifying the PizzaDirector or the Pizza class.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: The IPizzaBuilder interface defines the steps for constructing a pizza. It includes methods for setting the dough, sauce, and adding toppings, as well as a Build() method to return the fully constructed pizza. public interface IPizzaBuilder
{ void SetDough(string dough); void SetSauce(string sauce); void AddTopping(string topping); Pizza Build(); }
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: The separation of concerns means that any changes to the rendering logic (e.g., upgrading the drawing API) do not affect the shape logic, and vice versa. This results in less risk of introducing bugs when making changes.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: The IDrawingAPI interface defines the drawing method (DrawCircle in this case). Different concrete implementations of this interface will represent various drawing styles or technologies.
public interface IDrawingAPI
{ void DrawCircle(double x, double y, double radius); }
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: The pattern ensures that the UI components are consistent with the platform's look and feel, as each factory provides platform-specific products.
Improvement Suggestions: Extendability: You can extend this pattern by adding more UI components (e.g., menus, dialogs) to your abstract factory.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: The WindowsUIFactory and MacUIFactory are concrete implementations of the IUIFactory interface. Each factory creates platform-specific objects like buttons and checkboxes. public class WindowsUIFactory : IUIFactory
{
public IButton CreateButton() => new WindowsButton();
public ICheckbox CreateCheckbox() => new WindowsCheckbox();
}
public class MacUIFactory : IUIFactory
{
public IButton CreateButton() => new MacButton();
public ICheckbox CreateCheckbox() => new MacCheckbox();
}
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: ction and then set the next state, allowing the traffic light to cycle through its states (Red → Green → Yellow → Red). Benefits of the State Pattern:
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: dditional checks like authentication or caching. Conclusion: The Proxy Pattern is an excellent design pattern to control access to expensive or sensitive objects.
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: ccess to the real object. For example, it could authenticate users before llowing access to a sensitive resource. Types of Proxy:
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: nimals) can be cloned from a prototype, allowing for rapid creation of multiple instances with different states.
Deep Cloning Example: If you need to perform deep cloning, where not just the properties but also the referenced objects are cloned, you can adjust the Clone() method to handle the deep copy: public class GameCharacter : ICloneable { public string Name { get; set; } public int Health { get; set; } public List<string> Inventory { get; set; } = new List<string>(); public ICloneable Clone() { var clone = new GameCharacter { Name = this.Name, Health = this.Health, Inventory = new List<string>(this.Inventory) // Deep copy of the Inventory list }; return clone; } } In this case, the Inventory list will also be cloned to ensure that modifications to the Inventory of the clone do not affect the…… public int Health { get; set; }
public List<string> Inventory { get; set; } = new
List<string>();
public ICloneable Clone()
{
var clone = new GameCharacter
{ Name = this.Name, Health = this.Health, Inventory = new List<string>(this.Inventory) // Deep copy of the Inventory list }; return clone;
}
} In this case, the Inventory list will also be cloned to ensure that modifications to the Inventory of the clone do not affect the original object. Conclusion: The Prototype Pattern is a powerful creational pattern that allows you to clone objects instead of creating them from scratch. It's especially useful when dealing with complex objects or systems where performance and resource management are important. By using this pattern, you can quickly create new objects with similar attributes and save time and resources that would otherwise be spent constructing them from scratch. Proxy Pattern: Controlling Access to Expensive Resources Definition: The Proxy Pattern provides a surrogate or placeholder for another object to control access to it. The proxy acts as an intermediary, enabling you to perform additional actions (e.g., lazy loading, access control, logging) before or after delegating operations to the real object. Use Case: The Proxy Pattern is useful when you need to control access to an expensive or resource-intensive object. A common use case is controlling access to resources like large images, network connections, or database connections. Instead of creating the actual object immediately, a proxy can delay its creation or manage its lifecycle efficiently. Code Breakdown:
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: And implements the Update() method to receive news updates from the publisher.
public class NewsSubscriber : IObserver { private readonly string _name; public NewsSubscriber(string name) => _name = name; public void Update(string news) => Console.WriteLine($"{_name} received news: {news}"); } And implements the Update() method to receive news updates from the publisher. public class NewsSubscriber : IObserver { private readonly string _name; public NewsSubscriber(string name) => _name = name;
public void Update(string news) => Console.WriteLine($"{_name} received news: {news}"); }
When order status changes, observers update email, SMS, and analytics without the Order class knowing each one.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: re passed along the pipeline in a controlled and coordinated manner. Considerations and Drawbacks:
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: s an expression, and the pattern allows for flexible and extensible parsing rules. Improvement Suggestions:
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: ny component and let the pattern take care of the rest. Real-Time Use Case Example:
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: By adding new visitors (e.g., another visitor for tax calculation or shipping fees), you can introduce new operations on the same set of elements without modifying their classes. This is the key benefit of the Visitor Pattern. Benefits of the Visitor Pattern:
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: The concrete visitor ShoppingCart implements the IShoppingCartVisitor interface. It performs specific operations on each element, like calculating the total cost with any applicable discounts. For instance, it applies a 10% discount to books but no discount to fruits. public class ShoppingCart : IShoppingCartVisitor
{
private double _total;
public void Visit(Book book) => _total += book.Price * 0.9; // 10% discount on books public void Visit(Fruit fruit) => _total += fruit.Price; // No discount on fruits public double GetTotal() => _total;
}
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.
Install Toolliyo like an app Free
Home-screen access to tutorials, coding practice & career tools — no app store needed.
On iPhone/iPad: tap Share then Add to Home Screen.