<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Aniket Vaishnav</title>
    <description>The latest articles on Forem by Aniket Vaishnav (@obrutus).</description>
    <link>https://forem.com/obrutus</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F467786%2Fc4e6a2ad-6c73-4b2f-9974-3be49622f87f.jpg</url>
      <title>Forem: Aniket Vaishnav</title>
      <link>https://forem.com/obrutus</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/obrutus"/>
    <language>en</language>
    <item>
      <title>What If the OOD Interview Doesn’t Go as Planned?</title>
      <dc:creator>Aniket Vaishnav</dc:creator>
      <pubDate>Sun, 04 Jan 2026 20:33:44 +0000</pubDate>
      <link>https://forem.com/obrutus/what-if-the-ood-interview-doesnt-go-as-planned-344l</link>
      <guid>https://forem.com/obrutus/what-if-the-ood-interview-doesnt-go-as-planned-344l</guid>
      <description>&lt;p&gt;No matter how well you prepare, real interviews rarely follow a perfectly linear path. You might face curveballs such as shifting requirements, unexpected deep dives, or even a disengaged interviewer. The key is to stay adaptable, communicate clearly, and remain focused on delivering a thoughtful design.&lt;/p&gt;

&lt;p&gt;This section explores common challenges during OOD interviews and how to handle them with confidence and grace.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Shifting Requirements and Expanding Scope
&lt;/h2&gt;

&lt;p&gt;In some interviews, the scope of the problem may expand as you go. You might be halfway through a design when the interviewer introduces new requirements or constraints. Don’t panic. This is often intentional.&lt;/p&gt;

&lt;p&gt;✅ What to do:&lt;/p&gt;

&lt;p&gt;Acknowledge the new requirement and briefly assess its impact.&lt;br&gt;
Explain how your current design can accommodate the change, or what trade-offs might be required.&lt;br&gt;
Be flexible but strategic. Adapt your solution without overhauling it unnecessarily.&lt;br&gt;
If the interviewer keeps expanding on a specific area, it’s likely that flexibility and scalability are part of what they’re testing.&lt;br&gt;
In some cases, the shifting scope may be a subtle hint that your current design has a blind spot. Take a moment to re-evaluate and be one step ahead by recognizing potential design flaws.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Being Pulled into a Deep Dive Too Early
&lt;/h2&gt;

&lt;p&gt;Sometimes, the interviewer may want to dive into details before you’ve mapped out the broader structure. If you go too deep too soon, you risk losing sight of the big picture and running out of time.&lt;/p&gt;

&lt;p&gt;✅ What to do:&lt;/p&gt;

&lt;p&gt;Set expectations early: “I’ll start with a high-level overview, then we can dive deeper where needed.”&lt;br&gt;
Periodically check in on time and structure.&lt;br&gt;
If you’re stuck in one area, say: “Here’s the direction I’d take for now. Completing the rest of the system will give me the context to refine this.”&lt;br&gt;
Don’t forget to circle back to that part later. It shows follow-through.&lt;br&gt;
Avoid premature optimization or over-specificity early in the interview, as these can derail your momentum.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Struggling to Communicate Your Thought Process
&lt;/h2&gt;

&lt;p&gt;Clear communication is as important as solid design. If your thoughts feel jumbled or hard to explain, it can weaken the impact of your solution.&lt;/p&gt;

&lt;p&gt;✅ What to do:&lt;/p&gt;

&lt;p&gt;Begin with a high-level summary of your system before diving into class-level details.&lt;br&gt;
“The system has three main components: A, B, and C. Here’s how they interact.”&lt;br&gt;
Use visuals. A class diagram or code skeleton can anchor the conversation.&lt;br&gt;
Focus on why you made design decisions rather than just describing what they are.&lt;br&gt;
Choose intuitive names for classes and methods. Good naming reduces the need for lengthy explanations and reinforces clarity.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Dealing with a Disengaged Interviewer
&lt;/h2&gt;

&lt;p&gt;Not every interviewer will give active feedback. If they appear disinterested, confused, or silent, don’t let it throw you off.&lt;/p&gt;

&lt;p&gt;✅ What to do:&lt;/p&gt;

&lt;p&gt;Politely ask for feedback to re-engage them: “Would it be helpful if I clarified anything or focused on a specific part of the design?”&lt;br&gt;
If that doesn’t work, let your work speak for itself. Focus on delivering clean diagrams or runnable code.&lt;br&gt;
Especially in code-heavy interviews, executing working code can demonstrate your competence more effectively than conversation alone.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. When Your Design Decisions Are Challenged
&lt;/h2&gt;

&lt;p&gt;It’s common for interviewers to challenge your choices. This isn’t a bad sign. It’s a chance to demonstrate your reasoning and adaptability.&lt;/p&gt;

&lt;p&gt;✅ What to do:&lt;/p&gt;

&lt;p&gt;Stay calm and explain your thought process.&lt;br&gt;
Use concrete examples or real-world analogies to support your point.&lt;br&gt;
If relevant, reference trade-offs using terms like time complexity, extensibility, or maintainability.&lt;br&gt;
Offer alternatives: “I considered both inheritance and composition. I chose inheritance here because…”&lt;br&gt;
If you’re unsure, it’s okay to pause or ask for clarification: “Would you mind giving a specific case where you see this approach falling short?”&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Encountering Unfamiliar Terminology
&lt;/h2&gt;

&lt;p&gt;If the interviewer uses a term or concept you’re not familiar with, it’s better to clarify than to guess.&lt;/p&gt;

&lt;p&gt;✅ What to do:&lt;/p&gt;

&lt;p&gt;Ask politely: “Could you clarify what you mean by that term?”&lt;br&gt;
Or show partial understanding and align: “My understanding of this concept is X. Please let me know how it differs in your context.”&lt;br&gt;
This approach shows humility and professionalism without undermining your credibility.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Struggling with the Right Level of Abstraction
&lt;/h2&gt;

&lt;p&gt;Not sure how much detail to go into? This is a common tension in OOD interviews. Going too broad can make your solution feel vague; going too deep too early wastes time.&lt;/p&gt;

&lt;p&gt;✅ What to do:&lt;/p&gt;

&lt;p&gt;Start with a general structure and layer in details as needed.&lt;br&gt;
Ask the interviewer what level of depth they’d like you to go into:&lt;br&gt;
“Would you prefer a high-level architecture here or a more detailed class breakdown?”&lt;br&gt;
Stay flexible, and be prepared to zoom in or out depending on the interviewer’s cues.&lt;br&gt;
Each OOD problem has its own natural complexity, whether it’s in abstraction, data modeling, or behavior logic. Over time, you’ll develop a sense for what to emphasize.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Addressing Concurrency in OOD Interviews
&lt;/h2&gt;

&lt;p&gt;Concurrency is an advanced topic that interviewers may bring up, often by asking how your system handles multiple users or processes accessing the same resources at the same time.&lt;/p&gt;

&lt;p&gt;A classic example is a ticket booking system, where the key concern is preventing double-bookings when multiple users attempt to select the same seat. This scenario is a great opportunity to demonstrate techniques like locking, optimistic locking, or using language-specific synchronization mechanisms and concurrent data structures.&lt;/p&gt;

&lt;p&gt;Keep your explanation concise and your implementation simple. In most interviews, a high-level description of your concurrency strategy, along with a brief code snippet to illustrate how you prevent race conditions, is more than enough.&lt;/p&gt;

&lt;p&gt;In some cases, your system itself may need to run concurrently. If you’re coding in Java, understanding classes like Thread, Runnable, Callable, and ExecutorService is valuable, as it helps you avoid reinventing concurrency from low-level primitives.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;The object-oriented design interview is about more than just technical skills. It’s about thinking clearly under pressure, communicating effectively, and applying OOP principles to build maintainable, scalable solutions.&lt;/p&gt;

&lt;p&gt;By breaking the process into manageable steps and learning how to navigate unexpected challenges, you’ll be well-prepared to handle even the most unpredictable interviews. With practice and the right mindset, you can turn curveballs into opportunities and leave a lasting impression.&lt;/p&gt;

</description>
      <category>design</category>
      <category>interview</category>
      <category>systemdesign</category>
      <category>ood</category>
    </item>
    <item>
      <title>Use Case Diagram</title>
      <dc:creator>Aniket Vaishnav</dc:creator>
      <pubDate>Thu, 01 Jan 2026 20:58:04 +0000</pubDate>
      <link>https://forem.com/obrutus/use-case-diagram-1m5a</link>
      <guid>https://forem.com/obrutus/use-case-diagram-1m5a</guid>
      <description>&lt;p&gt;Use case diagrams describe a set of actions (called use cases) that a system should or can perform in collaboration with one or more external users of the system (called actors). Each use case should provide some observable and valuable result to the actors.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use Case Diagrams describe the high-level functional behavior of the system.&lt;/li&gt;
&lt;li&gt;It answers what system does from the user point of view.&lt;/li&gt;
&lt;li&gt;Use case answers ‘What will the system do?’ and at the same time tells us ‘What will the system NOT do?’.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A use case illustrates a unit of functionality provided by the system. The primary purpose of the use case diagram is to help development teams visualize the functional requirements of a system, including the relationship of “actors” to the essential processes, as well as the relationships among different use cases.&lt;/p&gt;

&lt;p&gt;To illustrate a use case on a use case diagram, we draw an oval in the middle of the diagram and put the name of the use case in the center of the oval. To show an actor (indicating a system user) on a use-case diagram, we draw a stick figure to the left or right of the diagram.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe6m49g994r9arlb5mjna.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe6m49g994r9arlb5mjna.png" alt=" " width="800" height="541"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The different components of the use case diagram are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;System boundary&lt;/strong&gt;: A system boundary defines the scope and limits of the system. It is shown as a rectangle that spans all use cases of the system.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Actors&lt;/strong&gt;: An actor is an entity who performs specific actions. These roles are the actual business roles of the users in a given system. An actor interacts with a use case of the system. For example, in a banking system, the customer is one of the actors.
Use Case: Every business functionality is a potential use case. The use case should list the discrete business functionality specified in the problem statement.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Include&lt;/strong&gt;: Include relationship represents an invocation of one use case by another use case. From a coding perspective, it is like one function being called by another function.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Extend&lt;/strong&gt;: This relationship signifies that the extended use case will work exactly like the base use case, except that some new steps will be inserted in the extended use case.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;This was about usecase diagram, for Class diagram: &lt;a href="https://dev.to/obrutus/class-diagram-cmg"&gt;https://dev.to/obrutus/class-diagram-cmg&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Class Diagram</title>
      <dc:creator>Aniket Vaishnav</dc:creator>
      <pubDate>Thu, 01 Jan 2026 20:57:30 +0000</pubDate>
      <link>https://forem.com/obrutus/class-diagram-cmg</link>
      <guid>https://forem.com/obrutus/class-diagram-cmg</guid>
      <description>&lt;p&gt;Class diagram is the backbone of object-oriented modeling - it shows how different entities (people, things, and data) relate to each other. In other words, it shows the static structures of the system. &lt;/p&gt;

&lt;p&gt;A class diagram describes the attributes and operations of a class and also the constraints imposed on the system. Class diagrams are widely used in the modeling of object-oriented systems because they are the only UML diagrams that can be mapped directly to object-oriented languages.&lt;/p&gt;




&lt;p&gt;The purpose of the class diagram can be summarized as:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Analysis and design of the static view of an application;&lt;/li&gt;
&lt;li&gt;To describe the responsibilities of a system;&lt;/li&gt;
&lt;li&gt;To provide a base for component and deployment diagrams; and,&lt;/li&gt;
&lt;li&gt;Forward and reverse engineering.&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;A class is depicted in the class diagram as a rectangle with three horizontal sections, as shown in the figure below. The upper section shows the class’s name (Flight), the middle section contains the properties of the class, and the lower section contains the class’s operations (or “methods”).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7n3yoh81yahi4kbqd884.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7n3yoh81yahi4kbqd884.png" alt=" " width="800" height="542"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These are the different types of relationships between classes:&lt;/p&gt;

&lt;h2&gt;
  
  
  1. &lt;strong&gt;&lt;em&gt;Association&lt;/em&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;If two classes in a model need to communicate with each other,&lt;br&gt;
there must be a link between them. This link can be represented by an association. Associations can be represented in a class diagram by a line between these classes with an arrow indicating the navigation direction. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;By default associations are always assumed to be bi-directional; this means that both classes are aware of each other and their relationship. In the diagram below, the association between Pilot and FlightInstance is bi-directional, as both classes know each other. &lt;/li&gt;
&lt;li&gt;By contrast, in a uni-directional association, hence it is a uni-directional association&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6p9isg7bpzktgwlv0pgj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6p9isg7bpzktgwlv0pgj.png" alt=" " width="800" height="618"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. &lt;strong&gt;&lt;em&gt;Multiplicity&lt;/em&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Multiplicity indicates how many instances of a class participate&lt;br&gt;
in the relationship. It is a constraint that specifies the range of permitted cardinalities between two classes. For example, in the diagram below, one FlightInstance will have two Pilots, while a Pilot can have many FlightInstances. A ranged multiplicity can be expressed as “0…*” which means “zero to many" or as “2…4” which means “two to four”.&lt;/p&gt;

&lt;p&gt;We can indicate the multiplicity of an association by adding multiplicity adornments to the line denoting the association. The below diagram, demonstrates that a FlightInstance has exactly two Pilots but a Pilot can have many FlightInstances.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. &lt;strong&gt;&lt;em&gt;Aggregation&lt;/em&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Aggregation is a special type of association used to model a “whole to its parts” relationship. In a basic aggregation relationship, the lifecycle of a PART class is independent of the WHOLE class’s lifecycle. In other words, aggregation implies a relationship where the child can exist independently of the parent. In the above diagram, Aircraft can exist without Airline.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. &lt;strong&gt;&lt;em&gt;Composition&lt;/em&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The composition aggregation relationship is just another form of the aggregation relationship, but the child class’s instance lifecycle is&lt;br&gt;
dependent on the parent class’s instance lifecycle. In other words,&lt;br&gt;
Composition implies a relationship where the child cannot exist independent of the parent. In the above example, WeeklySchedule is composed in Flight which means when Flight lifecycle ends, WeeklySchedule automatically gets destroyed&lt;/p&gt;

&lt;h2&gt;
  
  
  5. &lt;strong&gt;&lt;em&gt;Generalization&lt;/em&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Generalization is the mechanism for combining similar classes of objects into a single, more general class. Generalization identifies commonalities among a set of entities. In the above diagram, Crew, Pilot, and Admin, all are Person.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. &lt;strong&gt;&lt;em&gt;Dependency&lt;/em&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;A dependency relationship is a relationship in which one class, the client, uses or depends on another class, the supplier. In the above diagram, FlightReservation depends on Payment.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Abstract class: An abstract class is identified by specifying its name in &lt;em&gt;italics&lt;/em&gt;. In the above diagram, both Person and Account classes are abstract classes.
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftwtv2hm891hmstvyhch9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftwtv2hm891hmstvyhch9.png" alt=" " width="800" height="689"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For sequence diagram: &lt;a href="https://dev.to/obrutus/sequence-diagram-3nk8"&gt;https://dev.to/obrutus/sequence-diagram-3nk8&lt;/a&gt;&lt;/p&gt;

</description>
      <category>classdiagram</category>
      <category>lld</category>
      <category>uml</category>
    </item>
    <item>
      <title>Sequence diagram</title>
      <dc:creator>Aniket Vaishnav</dc:creator>
      <pubDate>Thu, 01 Jan 2026 20:57:02 +0000</pubDate>
      <link>https://forem.com/obrutus/sequence-diagram-3nk8</link>
      <guid>https://forem.com/obrutus/sequence-diagram-3nk8</guid>
      <description>&lt;p&gt;Sequence diagrams describe interactions among classes in terms of an&lt;br&gt;
exchange of messages over time and are used to explore the logic of complex operations, functions or procedures. In this diagram, the sequence of interactions between the objects is represented in a step-by-step manner. Sequence diagrams show a detailed flow for a specific use case or even just part of a particular use case. They are almost self-explanatory; they show the calls between the different objects in their sequence and can explain, at a detailed level, different calls to various objects.&lt;/p&gt;

&lt;p&gt;A sequence diagram has two dimensions: The vertical dimension shows the sequence of messages in the chronological order that they occur; the horizontal dimension shows the object instances to which the messages are sent.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5od8t8j28s3t4r84kh3l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5od8t8j28s3t4r84kh3l.png" alt=" " width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A sequence diagram is straightforward to draw. Across the top of your&lt;br&gt;
diagram, identify the class instances (objects) by putting each class instance inside a box (see above figure). If a class instance sends a message to another class instance, draw a line with an open arrowhead pointing to the receiving class instance and place the name of the message above the line. Optionally, for important messages, you can draw a dotted line with an arrowhead pointing back to the originating class instance; label the returned value above the dotted line.&lt;/p&gt;




&lt;p&gt;What is Activity Diagram know here : &lt;a href="https://dev.to/obrutus/activity-diagram-1obc"&gt;https://dev.to/obrutus/activity-diagram-1obc&lt;/a&gt;&lt;/p&gt;

</description>
      <category>uml</category>
      <category>lld</category>
      <category>sequencediagram</category>
    </item>
    <item>
      <title>Activity Diagram</title>
      <dc:creator>Aniket Vaishnav</dc:creator>
      <pubDate>Thu, 01 Jan 2026 20:56:37 +0000</pubDate>
      <link>https://forem.com/obrutus/activity-diagram-1obc</link>
      <guid>https://forem.com/obrutus/activity-diagram-1obc</guid>
      <description>&lt;p&gt;We use Activity Diagrams to illustrate the flow of control in a system. An activity diagram shows the flow of control for a system functionality; it emphasizes the condition of flow and the sequence in which it happens. We can also use an activity diagram to refer to the steps involved in the execution of a use case.&lt;/p&gt;

&lt;p&gt;Activity diagrams illustrate the dynamic nature of a system by modeling the flow of control from activity to activity. An activity represents an operation on some class in the system that results in a change in the state of the system. Typically, activity diagrams are used to model workflow or business processes and internal operations.&lt;/p&gt;

&lt;p&gt;Following is an activity diagram for a user performing online shopping:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Febdqlq865brb7nb15nbh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Febdqlq865brb7nb15nbh.png" alt=" " width="800" height="1290"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;What is the difference between Activity diagram and Sequence&lt;br&gt;
diagram?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Activity diagram&lt;/strong&gt; captures the process flow. It is used for functional modeling. A functional model represents the flow of values from external inputs, through operations and internal data stores, to external outputs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sequence diagram&lt;/strong&gt; tracks the interaction between the objects. It is used for dynamic modeling, which is represented by tracking states, transitions between states, and the events that trigger these transitions.&lt;/p&gt;

</description>
      <category>activitydiagram</category>
      <category>uml</category>
      <category>lld</category>
    </item>
    <item>
      <title>OO Analysis and Design</title>
      <dc:creator>Aniket Vaishnav</dc:creator>
      <pubDate>Wed, 31 Dec 2025 18:28:45 +0000</pubDate>
      <link>https://forem.com/obrutus/oo-analysis-and-design-1h5g</link>
      <guid>https://forem.com/obrutus/oo-analysis-and-design-1h5g</guid>
      <description>&lt;p&gt;OO Analysis and Design is a structured method for analyzing and designing a&lt;br&gt;
system by applying object-oriented concepts. This design process consists of&lt;br&gt;
an investigation into the objects constituting the system. It starts by first&lt;br&gt;
identifying the objects of the system and then figuring out the interactions&lt;br&gt;
between various objects.&lt;/p&gt;




&lt;p&gt;The process of OO analysis and design can be described as:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Identifying the objects in a system;&lt;/li&gt;
&lt;li&gt;Defining relationships between objects;&lt;/li&gt;
&lt;li&gt;Establishing the interface of each object; and,&lt;/li&gt;
&lt;li&gt;Making a design, which can be converted to executables using OO languages.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We need a standard method/tool to document all this information; for this purpose we use UML. UML can be considered as the successor of object-oriented (OO) analysis and design. UML is powerful enough to represent all the concepts that exist in object-oriented analysis and design. &lt;/p&gt;

&lt;p&gt;UML diagrams are a representation of object-oriented concepts only. Thus, before learning UML, it is essential to understand OO concepts.&lt;br&gt;
Let’s find out how we can model using UML.&lt;/p&gt;

&lt;p&gt;Get to know UML here : &lt;a href="https://dev.to/obrutus/what-is-uml-29p4"&gt;https://dev.to/obrutus/what-is-uml-29p4&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>What is UML?</title>
      <dc:creator>Aniket Vaishnav</dc:creator>
      <pubDate>Wed, 31 Dec 2025 18:28:16 +0000</pubDate>
      <link>https://forem.com/obrutus/what-is-uml-29p4</link>
      <guid>https://forem.com/obrutus/what-is-uml-29p4</guid>
      <description>&lt;p&gt;UML stands for Unified Modeling Language and is used to model the Object-Oriented Analysis of a software system. UML is a way of visualizing and documenting a software system by using a collection of diagrams, which helps engineers, businesspeople, and system architects understand the behavior and structure of the system being designed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Benefits of using UML:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Helps develop a quick understanding of a software system.&lt;/li&gt;
&lt;li&gt;UML modeling helps in breaking a complex system into discrete pieces
that can be easily understood.&lt;/li&gt;
&lt;li&gt;UML’s graphical notations can be used to communicate design decisions.&lt;/li&gt;
&lt;li&gt;Since UML is independent of any specific platform or language or
technology, it is easier to abstract out concepts.&lt;/li&gt;
&lt;li&gt;It becomes easier to hand the system over to a new team.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Get to know types of UML diagrams here : &lt;a href="https://dev.to/obrutus/types-of-uml-diagrams-40o8"&gt;https://dev.to/obrutus/types-of-uml-diagrams-40o8&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>development</category>
    </item>
    <item>
      <title>Types of UML Diagrams</title>
      <dc:creator>Aniket Vaishnav</dc:creator>
      <pubDate>Wed, 31 Dec 2025 18:26:23 +0000</pubDate>
      <link>https://forem.com/obrutus/types-of-uml-diagrams-40o8</link>
      <guid>https://forem.com/obrutus/types-of-uml-diagrams-40o8</guid>
      <description>&lt;p&gt;The current UML standards call for 14 different kinds of diagrams.&lt;br&gt;
These diagrams are organized into two distinct groups:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;structural&lt;/strong&gt; diagrams and &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;behavioral&lt;/strong&gt; or interaction diagrams. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As the names suggest, some UML diagrams analyze and depict the structure of a system or process, whereas others describe the behavior of the system, its actors, and its building components.&lt;/p&gt;

&lt;p&gt;The different types are broken down as follows:&lt;/p&gt;

&lt;h2&gt;
  
  
  Structural UML diagrams
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Class diagram&lt;/li&gt;
&lt;li&gt;Object diagram&lt;/li&gt;
&lt;li&gt;Package diagram&lt;/li&gt;
&lt;li&gt;Component diagram&lt;/li&gt;
&lt;li&gt;Composite structure diagram&lt;/li&gt;
&lt;li&gt;Deployment diagram&lt;/li&gt;
&lt;li&gt;Profile diagram&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Behavioral UML diagrams
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Use case diagram&lt;/li&gt;
&lt;li&gt;Activity diagram&lt;/li&gt;
&lt;li&gt;Sequence diagram&lt;/li&gt;
&lt;li&gt;State diagram&lt;/li&gt;
&lt;li&gt;Communication diagram&lt;/li&gt;
&lt;li&gt;Interaction overview diagram&lt;/li&gt;
&lt;li&gt;Timing diagram&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Well the most commonly is debatable, but the must known for anyone should be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use Case Diagram&lt;/strong&gt;: Used to describe a set of user scenarios, this diagram, illustrates the functionality provided by the system.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Class Diagram&lt;/strong&gt;: Used to describe structure and behavior in the use cases, this diagram provides a conceptual model of the system in terms of entities and their relationships.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Activity Diagram&lt;/strong&gt;: Used to model the functional flow-of-control between two or more class objects.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Sequence Diagram&lt;/strong&gt;: Used to describe interactions among classes in terms of an exchange of messages over time.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>architecture</category>
      <category>design</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>The four principles of object-oriented programming</title>
      <dc:creator>Aniket Vaishnav</dc:creator>
      <pubDate>Wed, 31 Dec 2025 18:03:52 +0000</pubDate>
      <link>https://forem.com/obrutus/the-four-principles-of-object-oriented-programming-mcc</link>
      <guid>https://forem.com/obrutus/the-four-principles-of-object-oriented-programming-mcc</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;The four principles of object-oriented programming are encapsulation,&lt;br&gt;
abstraction, inheritance, and polymorphism.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  1. Encapsulation
&lt;/h2&gt;

&lt;p&gt;Encapsulation is the mechanism of binding the data&lt;br&gt;
together and hiding it from the outside world. Encapsulation is achieved&lt;br&gt;
when each object keeps its state private so that other objects don’t have&lt;br&gt;
direct access to its state. Instead, they can access this state only through a&lt;br&gt;
set of public functions.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Abstraction
&lt;/h2&gt;

&lt;p&gt;Abstraction can be thought of as the natural extension of&lt;br&gt;
encapsulation. It means hiding all but the relevant data about an object&lt;br&gt;
in order to reduce the complexity of the system. In a large system, objects&lt;br&gt;
talk to each other, which makes it difficult to maintain a large code base;&lt;br&gt;
abstraction helps by hiding internal implementation details of objects&lt;br&gt;
and only revealing operations that are relevant to other objects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Inheritance
&lt;/h2&gt;

&lt;p&gt;Inheritance is the mechanism of creating new classes from existing ones.&lt;/p&gt;

&lt;h2&gt;
  
  
  Polymorphism
&lt;/h2&gt;

&lt;p&gt;Polymorphism (from Greek, meaning “many forms”)&lt;br&gt;
is the ability of an object to take different forms and thus, depending&lt;br&gt;
upon the context, to respond to the same message in different ways. Take&lt;br&gt;
the example of a chess game; a chess piece can take many forms, like&lt;br&gt;
bishop, castle, or knight and all these pieces will respond differently to&lt;br&gt;
the ‘move’ message.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>architecture</category>
      <category>computerscience</category>
      <category>programming</category>
    </item>
    <item>
      <title>Words and Letters: Take your side</title>
      <dc:creator>Aniket Vaishnav</dc:creator>
      <pubDate>Thu, 18 Dec 2025 16:57:09 +0000</pubDate>
      <link>https://forem.com/obrutus/words-and-letters-take-your-side-2lfk</link>
      <guid>https://forem.com/obrutus/words-and-letters-take-your-side-2lfk</guid>
      <description>&lt;p&gt;Hate has 4 letters so does Love&lt;br&gt;
Enemies has 7 letters so does Friends&lt;br&gt;
Lying has 5 letters so does Truth&lt;br&gt;
Failure has 7 letters so does Success&lt;br&gt;
Cry has 3 letters so does Joy&lt;br&gt;
and&lt;br&gt;
Negativity has 10 letters so does Positivity&lt;/p&gt;

&lt;p&gt;You always have a choice so choose the better side of it!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn9ol58yowlc0wn0a5jew.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn9ol58yowlc0wn0a5jew.png" alt="Vagabond Image" width="777" height="673"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>motivation</category>
    </item>
    <item>
      <title>Cosine Similarity Explained — Intuitively and Practically</title>
      <dc:creator>Aniket Vaishnav</dc:creator>
      <pubDate>Sat, 14 Jun 2025 11:46:25 +0000</pubDate>
      <link>https://forem.com/obrutus/cosine-similarity-explained-intuitively-and-practically-44md</link>
      <guid>https://forem.com/obrutus/cosine-similarity-explained-intuitively-and-practically-44md</guid>
      <description>&lt;p&gt;Ever wondered what &lt;strong&gt;cosine similarity&lt;/strong&gt; really means and how it works?&lt;/p&gt;

&lt;p&gt;Let’s break it down in a way that’s simple, intuitive, and practical — so the next time you hear it in a machine learning conversation, you won’t just nod along, you’ll &lt;em&gt;own&lt;/em&gt; it.&lt;/p&gt;




&lt;h2&gt;
  
  
  🚀 What Is Cosine Similarity?
&lt;/h2&gt;

&lt;p&gt;According to Wikipedia:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Cosine similarity is a measure of similarity between two non-zero vectors defined in an inner product space.&lt;/p&gt;

&lt;p&gt;It is the cosine of the angle between the vectors.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F0%2ACV3hqWct7JJNJtgs" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F0%2ACV3hqWct7JJNJtgs" alt="Cosine Similarity Diagram" width="1600" height="1200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That’s technically correct — but what does it &lt;strong&gt;really&lt;/strong&gt; mean?&lt;/p&gt;

&lt;p&gt;Let’s interpret this with real-world clarity.&lt;/p&gt;




&lt;h2&gt;
  
  
  🎯 Understanding the Intuition
&lt;/h2&gt;

&lt;p&gt;Similarity metrics (like Euclidean or Manhattan distance) typically measure &lt;strong&gt;how far apart&lt;/strong&gt; two data points are. The closer they are, the more similar they’re considered.&lt;/p&gt;

&lt;p&gt;Cosine similarity, however, takes a &lt;strong&gt;different approach&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Imagine vectors as arrows from the origin in space. Instead of looking at their length (magnitude), cosine similarity compares their &lt;strong&gt;direction&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So, even if two vectors have different lengths, if they &lt;strong&gt;point in the same direction&lt;/strong&gt;, they’re considered similar.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This makes cosine similarity perfect for &lt;strong&gt;text data&lt;/strong&gt;, &lt;strong&gt;high-dimensional data&lt;/strong&gt;, and &lt;strong&gt;preference vectors&lt;/strong&gt;, where &lt;em&gt;pattern matters more than size&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  🧠 Quick Refresher: Vectors and Angles
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;When two vectors point in the &lt;strong&gt;same direction&lt;/strong&gt;, the angle between them is 0°, and cosine similarity is &lt;strong&gt;1&lt;/strong&gt; (perfect match).&lt;/li&gt;
&lt;li&gt;When the angle is 90°, cosine similarity is &lt;strong&gt;0&lt;/strong&gt; (no similarity).&lt;/li&gt;
&lt;li&gt;If they point in &lt;strong&gt;opposite directions&lt;/strong&gt;, cosine similarity becomes &lt;strong&gt;-1&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It’s all captured in this elegant formula:&lt;/p&gt;

&lt;p&gt;$$&lt;br&gt;
\text{cosine_similarity}(A, B) = \frac{A \cdot B}{|A| |B|} = \frac{\sum_{i=1}^{n} A_i B_i}{\sqrt{\sum_{i=1}^{n} A_i^2} \sqrt{\sum_{i=1}^{n} B_i^2}}&lt;br&gt;
$$&lt;/p&gt;

&lt;p&gt;Where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;( A \cdot B ) is the dot product&lt;/li&gt;
&lt;li&gt;( |A| ), ( |B| ) are vector magnitudes&lt;/li&gt;
&lt;li&gt;( n ) is the number of dimensions&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔍 Cosine Similarity Range and Interpretation
&lt;/h2&gt;

&lt;p&gt;Cosine similarity values range from &lt;strong&gt;-1 to +1&lt;/strong&gt;:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;th&gt;Interpretation&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;+1&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Identical direction (highly similar)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;~0&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;No directional similarity (orthogonal)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-1&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Opposite direction (completely dissimilar)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;In most real-world applications (especially with non-negative vectors like text frequencies), values fall between &lt;strong&gt;0 and 1&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  🧪 Real-World Examples
&lt;/h2&gt;

&lt;h3&gt;
  
  
  ✅ Documents
&lt;/h3&gt;

&lt;p&gt;Two documents with similar topics will have high cosine similarity between their word embeddings or TF-IDF vectors.&lt;/p&gt;

&lt;h3&gt;
  
  
  ✅ Recommendations
&lt;/h3&gt;

&lt;p&gt;Two users with similar viewing habits will have profile vectors pointing in similar directions — cosine similarity helps recommend new content.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9637z4ly6ojnaepwjmhq.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9637z4ly6ojnaepwjmhq.jpeg" alt="User Behavior Vector Alignment" width="800" height="568"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  🔧 Where Is Cosine Similarity Used?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. 🗣️ Natural Language Processing (NLP)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Comparing documents and sentences&lt;/li&gt;
&lt;li&gt;Search ranking and semantic similarity&lt;/li&gt;
&lt;li&gt;Identifying duplicates or near-duplicates&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. 🎯 Recommendation Engines
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;User-item similarity&lt;/li&gt;
&lt;li&gt;Collaborative filtering&lt;/li&gt;
&lt;li&gt;Finding similar products or users&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. 🧠 Machine Learning Algorithms
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Clustering (e.g., k-Means)&lt;/li&gt;
&lt;li&gt;Classification (e.g., k-NN)&lt;/li&gt;
&lt;li&gt;Embedding similarity (e.g., BERT, Word2Vec)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. 🖼️ Computer Vision
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Comparing image embeddings (e.g., face recognition)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  👥 Who Uses Cosine Similarity?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Data Scientists &amp;amp; ML Engineers&lt;/strong&gt;: for modeling similarity&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Search Engineers&lt;/strong&gt;: for information retrieval&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Recommender System Developers&lt;/strong&gt;: for user-item matching&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Researchers&lt;/strong&gt;: in NLP, biology, social networks&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Software Engineers&lt;/strong&gt;: in chatbots, personalization, vector DBs&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🎵 Final Analogy: Music Taste
&lt;/h2&gt;

&lt;p&gt;Imagine your and your friend's music preferences as vectors.&lt;/p&gt;

&lt;p&gt;Even if your friend listens to &lt;em&gt;more&lt;/em&gt; music, you both love jazz and dislike metal. Your preferences point in the same direction — that’s what cosine similarity captures: &lt;strong&gt;shared taste, not quantity&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  ✅ TL;DR — Why Cosine Similarity Rocks
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Ignores magnitude, focuses on &lt;strong&gt;directional similarity&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Perfect for &lt;strong&gt;text, embeddings, user behavior&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Ranges from &lt;strong&gt;-1 to +1&lt;/strong&gt;, intuitive and fast&lt;/li&gt;
&lt;li&gt;Used everywhere: &lt;strong&gt;NLP, recommendation, clustering&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  💬 Over to You
&lt;/h2&gt;

&lt;p&gt;Have you used cosine similarity in a project or algorithm? Drop a comment or share your experience!&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>beginners</category>
      <category>algorithms</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>📈 Best Time to Buy and Sell Stock (LeetCode 121)</title>
      <dc:creator>Aniket Vaishnav</dc:creator>
      <pubDate>Mon, 09 Jun 2025 10:24:15 +0000</pubDate>
      <link>https://forem.com/obrutus/best-time-to-buy-and-sell-stock-leetcode-121-2c7c</link>
      <guid>https://forem.com/obrutus/best-time-to-buy-and-sell-stock-leetcode-121-2c7c</guid>
      <description>&lt;h2&gt;
  
  
  🧠 Problem Statement
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=COJ-KY7DIOs" rel="noopener noreferrer"&gt;Video Explain&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You are given an array &lt;code&gt;prices&lt;/code&gt; where &lt;code&gt;prices[i]&lt;/code&gt; represents the price of a given stock on the &lt;code&gt;i-th&lt;/code&gt; day.&lt;/p&gt;

&lt;p&gt;Your goal is to &lt;strong&gt;maximize profit&lt;/strong&gt; by &lt;strong&gt;choosing a single day to buy one stock&lt;/strong&gt; and a &lt;strong&gt;different future day to sell&lt;/strong&gt; that stock.&lt;/p&gt;

&lt;p&gt;If no profitable transaction is possible, return &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  🧾 Examples
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Example 1:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: prices = [7, 1, 5, 3, 6, 4]
Output: 5
Explanation: Buy on day 2 (price = 1), sell on day 5 (price = 6). Profit = 6 - 1 = 5.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Example 2:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: prices = [7, 6, 4, 3, 1]
Output: 0
Explanation: No future price is higher than the earlier prices.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🔍 Constraints
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= prices.length &amp;lt;= 10^5&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0 &amp;lt;= prices[i] &amp;lt;= 10^4&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  💡 Intuition
&lt;/h2&gt;

&lt;p&gt;We need to &lt;strong&gt;buy low and sell high&lt;/strong&gt;, but with a key restriction:  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We &lt;strong&gt;must buy before we sell&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So we’re looking for the &lt;strong&gt;maximum difference&lt;/strong&gt; between two prices &lt;code&gt;prices[j] - prices[i]&lt;/code&gt; where &lt;code&gt;j &amp;gt; i&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  🪜 Step-by-Step Approach
&lt;/h2&gt;

&lt;h3&gt;
  
  
  🔁 One-Pass Greedy Algorithm
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Track the minimum price so far&lt;/strong&gt; as you iterate.&lt;/li&gt;
&lt;li&gt;For each price, calculate the &lt;strong&gt;profit&lt;/strong&gt; if you bought at the minimum and sold today.&lt;/li&gt;
&lt;li&gt;Keep updating the &lt;strong&gt;maximum profit&lt;/strong&gt; found.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  ✅ Why this works
&lt;/h3&gt;

&lt;p&gt;We always look &lt;strong&gt;backward&lt;/strong&gt; to see the best time we &lt;em&gt;could&lt;/em&gt; have bought the stock before today.&lt;br&gt;&lt;br&gt;
This ensures we never violate the "buy before sell" rule.&lt;/p&gt;




&lt;h3&gt;
  
  
  🧮 Dry Run
&lt;/h3&gt;

&lt;p&gt;Let's walk through &lt;code&gt;prices = [7, 1, 5, 3, 6, 4]&lt;/code&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Day&lt;/th&gt;
&lt;th&gt;Price&lt;/th&gt;
&lt;th&gt;Min Price So Far&lt;/th&gt;
&lt;th&gt;Profit if Sold Today&lt;/th&gt;
&lt;th&gt;Max Profit&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1 ✅&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;4 ✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;5 ✅&lt;/td&gt;
&lt;td&gt;5 ✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  🧑‍💻 C++ Code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;algorithm&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;maxProfit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;minPrice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;INT_MAX&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;maxProfit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;minPrice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;minPrice&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Track lowest price so far&lt;/span&gt;
        &lt;span class="n"&gt;maxProfit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;maxProfit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;minPrice&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Check today's profit&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;maxProfit&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🧑‍💻 Java Code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;maxProfit&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;prices&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;minPrice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;MAX_VALUE&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;maxProfit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;prices&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;minPrice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;min&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;minPrice&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Track lowest price so far&lt;/span&gt;
            &lt;span class="n"&gt;maxProfit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;max&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;maxProfit&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;minPrice&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Check today's profit&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;maxProfit&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  📌 Time &amp;amp; Space Complexity
&lt;/h2&gt;

&lt;p&gt;Metric  Value&lt;br&gt;
Time Complexity O(n)&lt;br&gt;
Space Complexity    O(1)&lt;/p&gt;

&lt;h2&gt;
  
  
  🔚 Final Thoughts
&lt;/h2&gt;

&lt;p&gt;This problem is a classic sliding window or greedy pattern.&lt;/p&gt;

&lt;p&gt;It teaches a powerful lesson: Track the best "buy" dynamically, rather than trying every pair.&lt;/p&gt;

&lt;p&gt;Let me know if you'd like&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=COJ-KY7DIOs" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=COJ-KY7DIOs&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
