<?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: Abhishek Gupta </title>
    <description>The latest articles on Forem by Abhishek Gupta  (@abhishek-nexgen-dev).</description>
    <link>https://forem.com/abhishek-nexgen-dev</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%2F2038086%2Fc3e0a0ad-d888-4dc1-ade6-3ee10b7fed08.jpg</url>
      <title>Forem: Abhishek Gupta </title>
      <link>https://forem.com/abhishek-nexgen-dev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/abhishek-nexgen-dev"/>
    <language>en</language>
    <item>
      <title>AI in Healthcare and Diagnostics</title>
      <dc:creator>Abhishek Gupta </dc:creator>
      <pubDate>Sat, 18 Apr 2026 17:18:48 +0000</pubDate>
      <link>https://forem.com/abhishek-nexgen-dev/ai-in-healthcare-and-diagnostics-4cb</link>
      <guid>https://forem.com/abhishek-nexgen-dev/ai-in-healthcare-and-diagnostics-4cb</guid>
      <description>&lt;h1&gt;
  
  
  What is AI in Healthcare?
&lt;/h1&gt;

&lt;p&gt;AI means smart computer systems.&lt;/p&gt;

&lt;p&gt;These systems can help doctors:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Find diseases&lt;/li&gt;
&lt;li&gt;Read medical scans&lt;/li&gt;
&lt;li&gt;Predict health risks&lt;/li&gt;
&lt;li&gt;Suggest treatments&lt;/li&gt;
&lt;li&gt;Monitor patients&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Think of AI like a smart assistant for doctors.&lt;/p&gt;

&lt;p&gt;It helps.&lt;/p&gt;

&lt;p&gt;It does not replace doctors.&lt;/p&gt;




&lt;h1&gt;
  
  
  Why Do We Need AI in Healthcare?
&lt;/h1&gt;

&lt;p&gt;Healthcare has many problems.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problems:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Too many patients&lt;/li&gt;
&lt;li&gt;Too few doctors&lt;/li&gt;
&lt;li&gt;Long waiting times&lt;/li&gt;
&lt;li&gt;High treatment costs&lt;/li&gt;
&lt;li&gt;Human mistakes&lt;/li&gt;
&lt;li&gt;Diseases found too late&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;AI helps solve these problems.&lt;/p&gt;




&lt;h1&gt;
  
  
  Problems Before AI
&lt;/h1&gt;

&lt;h2&gt;
  
  
  1. Slow Diagnosis
&lt;/h2&gt;

&lt;p&gt;Sometimes doctors need hours or days to find a disease.&lt;/p&gt;

&lt;p&gt;This can delay treatment.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Human Error
&lt;/h2&gt;

&lt;p&gt;Doctors can get tired.&lt;/p&gt;

&lt;p&gt;Sometimes diseases may be missed.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Doctor Shortage
&lt;/h2&gt;

&lt;p&gt;Many villages and rural areas do not have enough specialists.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. High Cost
&lt;/h2&gt;

&lt;p&gt;Traditional healthcare can be expensive.&lt;/p&gt;




&lt;h1&gt;
  
  
  How AI Helps
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Early Detection
&lt;/h2&gt;

&lt;p&gt;AI can help find disease early.&lt;/p&gt;

&lt;p&gt;Early treatment can save lives.&lt;/p&gt;




&lt;h2&gt;
  
  
  Faster Diagnosis
&lt;/h2&gt;

&lt;p&gt;AI can analyze scans much faster.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;Traditional:&lt;/p&gt;

&lt;p&gt;Hours.&lt;/p&gt;

&lt;p&gt;AI:&lt;/p&gt;

&lt;p&gt;Minutes.&lt;/p&gt;




&lt;h2&gt;
  
  
  Better Scan Reading
&lt;/h2&gt;

&lt;p&gt;AI helps read:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;X-rays&lt;/li&gt;
&lt;li&gt;CT scans&lt;/li&gt;
&lt;li&gt;MRI&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Risk Prediction
&lt;/h2&gt;

&lt;p&gt;AI can warn about:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stroke risk&lt;/li&gt;
&lt;li&gt;Heart disease risk&lt;/li&gt;
&lt;li&gt;Sepsis risk&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Real Examples
&lt;/h1&gt;

&lt;h1&gt;
  
  
  Cancer Detection
&lt;/h1&gt;

&lt;p&gt;AI can help detect:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Breast cancer&lt;/li&gt;
&lt;li&gt;Lung cancer&lt;/li&gt;
&lt;li&gt;Skin cancer&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It can find small signs doctors may miss.&lt;/p&gt;




&lt;h1&gt;
  
  
  Brain Tumor Detection
&lt;/h1&gt;

&lt;p&gt;Traditional method:&lt;/p&gt;

&lt;p&gt;Doctors check scans manually.&lt;/p&gt;

&lt;p&gt;It may take time.&lt;/p&gt;

&lt;p&gt;AI:&lt;/p&gt;

&lt;p&gt;Can analyze images much faster.&lt;/p&gt;




&lt;h1&gt;
  
  
  Heart Disease
&lt;/h1&gt;

&lt;p&gt;AI can analyze ECG signals.&lt;/p&gt;

&lt;p&gt;It may help detect heart problems early.&lt;/p&gt;




&lt;h1&gt;
  
  
  Diabetic Eye Disease
&lt;/h1&gt;

&lt;p&gt;AI can help prevent blindness.&lt;/p&gt;




&lt;h1&gt;
  
  
  Sepsis Detection
&lt;/h1&gt;

&lt;p&gt;Sepsis can be deadly.&lt;/p&gt;

&lt;p&gt;AI may warn doctors early.&lt;/p&gt;

&lt;p&gt;That can save lives.&lt;/p&gt;




&lt;h1&gt;
  
  
  Real Companies Using AI
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Google DeepMind
&lt;/h2&gt;

&lt;p&gt;Works on medical AI.&lt;/p&gt;




&lt;h2&gt;
  
  
  Aidoc
&lt;/h2&gt;

&lt;p&gt;Helps hospitals detect emergencies faster.&lt;/p&gt;




&lt;h2&gt;
  
  
  PathAI
&lt;/h2&gt;

&lt;p&gt;Helps in cancer diagnosis.&lt;/p&gt;




&lt;h2&gt;
  
  
  Tempus
&lt;/h2&gt;

&lt;p&gt;Uses AI for personalized medicine.&lt;/p&gt;




&lt;h1&gt;
  
  
  Traditional vs AI
&lt;/h1&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Traditional&lt;/th&gt;
&lt;th&gt;AI&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Speed&lt;/td&gt;
&lt;td&gt;Slow&lt;/td&gt;
&lt;td&gt;Fast&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cost&lt;/td&gt;
&lt;td&gt;Higher&lt;/td&gt;
&lt;td&gt;Can reduce cost&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Errors&lt;/td&gt;
&lt;td&gt;More possible&lt;/td&gt;
&lt;td&gt;Can reduce some errors&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Rural access&lt;/td&gt;
&lt;td&gt;Limited&lt;/td&gt;
&lt;td&gt;Better&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h1&gt;
  
  
  Advantages of AI
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Faster diagnosis
&lt;/h2&gt;

&lt;p&gt;Can save time.&lt;/p&gt;




&lt;h2&gt;
  
  
  Better accuracy
&lt;/h2&gt;

&lt;p&gt;Can help doctors detect disease.&lt;/p&gt;




&lt;h2&gt;
  
  
  Lower costs
&lt;/h2&gt;

&lt;p&gt;Can improve efficiency.&lt;/p&gt;




&lt;h2&gt;
  
  
  24/7 Support
&lt;/h2&gt;

&lt;p&gt;AI systems can work all day.&lt;/p&gt;




&lt;h2&gt;
  
  
  Remote Healthcare
&lt;/h2&gt;

&lt;p&gt;Can help people in villages.&lt;/p&gt;




&lt;h1&gt;
  
  
  Risks of AI
&lt;/h1&gt;

&lt;h2&gt;
  
  
  AI can make mistakes
&lt;/h2&gt;

&lt;p&gt;Wrong prediction is possible.&lt;/p&gt;




&lt;h2&gt;
  
  
  Data privacy risk
&lt;/h2&gt;

&lt;p&gt;Medical data must be protected.&lt;/p&gt;




&lt;h2&gt;
  
  
  Bias problem
&lt;/h2&gt;

&lt;p&gt;If data is bad,&lt;/p&gt;

&lt;p&gt;AI may give bad results.&lt;/p&gt;




&lt;h1&gt;
  
  
  Can AI Replace Doctors?
&lt;/h1&gt;

&lt;p&gt;No.&lt;/p&gt;

&lt;p&gt;AI helps doctors.&lt;/p&gt;

&lt;p&gt;Doctors still make final decisions.&lt;/p&gt;

&lt;p&gt;Humans are necessary.&lt;/p&gt;




&lt;h1&gt;
  
  
  Diseases Where AI Is Used
&lt;/h1&gt;

&lt;p&gt;AI helps in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cancer&lt;/li&gt;
&lt;li&gt;Stroke&lt;/li&gt;
&lt;li&gt;Diabetes&lt;/li&gt;
&lt;li&gt;Tuberculosis&lt;/li&gt;
&lt;li&gt;Pneumonia&lt;/li&gt;
&lt;li&gt;Brain disease&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Future of AI in Healthcare
&lt;/h1&gt;

&lt;p&gt;Future may include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Smart hospitals&lt;/li&gt;
&lt;li&gt;AI surgery support&lt;/li&gt;
&lt;li&gt;Wearables with AI&lt;/li&gt;
&lt;li&gt;Better drug discovery&lt;/li&gt;
&lt;li&gt;Rural AI healthcare&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Challenges in India and Developing Countries
&lt;/h1&gt;

&lt;p&gt;Problems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fewer doctors&lt;/li&gt;
&lt;li&gt;Big rural population&lt;/li&gt;
&lt;li&gt;Limited hospitals&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;AI may help improve access.&lt;/p&gt;




&lt;h1&gt;
  
  
  Debate
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Good Side
&lt;/h2&gt;

&lt;p&gt;AI can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Save time&lt;/li&gt;
&lt;li&gt;Save lives&lt;/li&gt;
&lt;li&gt;Improve care&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Risk Side
&lt;/h2&gt;

&lt;p&gt;AI can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Make errors&lt;/li&gt;
&lt;li&gt;Cause privacy risks&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Balanced Answer
&lt;/h1&gt;

&lt;p&gt;AI is powerful.&lt;/p&gt;

&lt;p&gt;But it must be used carefully.&lt;/p&gt;




&lt;h1&gt;
  
  
  Common Questions
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Is AI safe?
&lt;/h2&gt;

&lt;p&gt;It can help.&lt;/p&gt;

&lt;p&gt;But it needs human oversight.&lt;/p&gt;




&lt;h2&gt;
  
  
  Can AI make mistakes?
&lt;/h2&gt;

&lt;p&gt;Yes.&lt;/p&gt;




&lt;h2&gt;
  
  
  Can AI replace doctors?
&lt;/h2&gt;

&lt;p&gt;No.&lt;/p&gt;




&lt;h1&gt;
  
  
  Best 10 Slides for Presentation
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Slide 1
&lt;/h2&gt;

&lt;p&gt;What is AI in Healthcare?&lt;/p&gt;




&lt;h2&gt;
  
  
  Slide 2
&lt;/h2&gt;

&lt;p&gt;Problems in Traditional Healthcare&lt;/p&gt;




&lt;h2&gt;
  
  
  Slide 3
&lt;/h2&gt;

&lt;p&gt;How AI Solves These Problems&lt;/p&gt;




&lt;h2&gt;
  
  
  Slide 4
&lt;/h2&gt;

&lt;p&gt;Real Examples&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cancer&lt;/li&gt;
&lt;li&gt;Stroke&lt;/li&gt;
&lt;li&gt;Sepsis&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Slide 5
&lt;/h2&gt;

&lt;p&gt;Traditional vs AI Comparison&lt;/p&gt;




&lt;h2&gt;
  
  
  Slide 6
&lt;/h2&gt;

&lt;p&gt;Advantages of AI&lt;/p&gt;




&lt;h2&gt;
  
  
  Slide 7
&lt;/h2&gt;

&lt;p&gt;Risks of AI&lt;/p&gt;




&lt;h2&gt;
  
  
  Slide 8
&lt;/h2&gt;

&lt;p&gt;Future Scope&lt;/p&gt;




&lt;h2&gt;
  
  
  Slide 9
&lt;/h2&gt;

&lt;p&gt;Can AI Replace Doctors?&lt;/p&gt;




&lt;h2&gt;
  
  
  Slide 10
&lt;/h2&gt;

&lt;p&gt;Conclusion&lt;/p&gt;

</description>
    </item>
    <item>
      <title>CRDT (Conflict-free Replicated Data Types) — Easy Notes for Exams + Interviews + Real Understanding</title>
      <dc:creator>Abhishek Gupta </dc:creator>
      <pubDate>Sat, 18 Apr 2026 13:06:17 +0000</pubDate>
      <link>https://forem.com/abhishek-nexgen-dev/crdt-conflict-free-replicated-data-types-easy-notes-for-exams-interviews-real-understanding-1ba7</link>
      <guid>https://forem.com/abhishek-nexgen-dev/crdt-conflict-free-replicated-data-types-easy-notes-for-exams-interviews-real-understanding-1ba7</guid>
      <description>&lt;h2&gt;
  
  
  1. What is CRDT? (Simple Definition)
&lt;/h2&gt;

&lt;p&gt;CRDT stands for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;C = Conflict&lt;/li&gt;
&lt;li&gt;R = Replicated&lt;/li&gt;
&lt;li&gt;D = Data&lt;/li&gt;
&lt;li&gt;T = Type&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Full form:
&lt;/h2&gt;

&lt;p&gt;Conflict-free Replicated Data Types&lt;/p&gt;

&lt;h2&gt;
  
  
  Easy definition:
&lt;/h2&gt;

&lt;p&gt;CRDT is a special data structure that allows many users to edit the same data at the same time &lt;strong&gt;without conflicts&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;All users eventually see the same final data.&lt;/p&gt;




&lt;h2&gt;
  
  
  Interview Definition
&lt;/h2&gt;

&lt;p&gt;CRDT is a distributed data structure that lets multiple replicas update independently and automatically merge changes without conflicts.&lt;/p&gt;




&lt;h2&gt;
  
  
  One-line exam answer
&lt;/h2&gt;

&lt;p&gt;CRDT is a technique used in distributed systems for conflict-free synchronization of shared data.&lt;/p&gt;




&lt;h1&gt;
  
  
  2. Why do we need CRDT?
&lt;/h1&gt;

&lt;p&gt;Suppose two users edit same document.&lt;/p&gt;

&lt;p&gt;User A:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;User B:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello World
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Problem:&lt;/p&gt;

&lt;p&gt;Which version should stay?&lt;/p&gt;

&lt;p&gt;This is called:&lt;/p&gt;

&lt;h2&gt;
  
  
  Conflict.
&lt;/h2&gt;

&lt;p&gt;CRDT solves this automatically.&lt;/p&gt;




&lt;h1&gt;
  
  
  3. Why is it called conflict-free?
&lt;/h1&gt;

&lt;p&gt;Because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No manual merge needed&lt;/li&gt;
&lt;li&gt;No data overwrite&lt;/li&gt;
&lt;li&gt;No edit loss&lt;/li&gt;
&lt;li&gt;Changes merge automatically&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That is “conflict-free”.&lt;/p&gt;




&lt;h1&gt;
  
  
  4. What does replicated mean?
&lt;/h1&gt;

&lt;p&gt;Replicated means:&lt;/p&gt;

&lt;p&gt;Same data exists in multiple places.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User A browser&lt;/li&gt;
&lt;li&gt;User B browser&lt;/li&gt;
&lt;li&gt;Server&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All have a copy.&lt;/p&gt;

&lt;p&gt;These copies are called:&lt;/p&gt;

&lt;p&gt;Replicas.&lt;/p&gt;




&lt;h1&gt;
  
  
  5. Real-life examples using CRDT
&lt;/h1&gt;

&lt;p&gt;CRDT ideas are used in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Google Docs&lt;/li&gt;
&lt;li&gt;Figma&lt;/li&gt;
&lt;li&gt;Notion&lt;/li&gt;
&lt;li&gt;VS Code Live Share&lt;/li&gt;
&lt;li&gt;Real-time code editors&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  6. How does CRDT work? (Easy Steps)
&lt;/h1&gt;

&lt;p&gt;Step 1:&lt;/p&gt;

&lt;p&gt;User types.&lt;/p&gt;

&lt;p&gt;Step 2:&lt;/p&gt;

&lt;p&gt;Change is stored as operation.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Insert A
Delete B
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 3:&lt;/p&gt;

&lt;p&gt;Operation sent to other users.&lt;/p&gt;

&lt;p&gt;Step 4:&lt;/p&gt;

&lt;p&gt;Everyone applies same updates.&lt;/p&gt;

&lt;p&gt;Step 5:&lt;/p&gt;

&lt;p&gt;All copies become same.&lt;/p&gt;

&lt;p&gt;This is called:&lt;/p&gt;

&lt;h2&gt;
  
  
  Convergence
&lt;/h2&gt;




&lt;h1&gt;
  
  
  7. What is convergence?
&lt;/h1&gt;

&lt;p&gt;Convergence means:&lt;/p&gt;

&lt;p&gt;All users finally see same result.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;User A sees:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello World
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;User B sees:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello World
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same final state.&lt;/p&gt;

&lt;p&gt;That is convergence.&lt;/p&gt;




&lt;h1&gt;
  
  
  8. Two types of CRDT
&lt;/h1&gt;

&lt;h2&gt;
  
  
  1. State-based CRDT
&lt;/h2&gt;

&lt;p&gt;Whole state is shared.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;Send full document.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Operation-based CRDT
&lt;/h2&gt;

&lt;p&gt;Only operations shared.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Insert X
Delete Y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Most editors use this idea.&lt;/p&gt;




&lt;h1&gt;
  
  
  9. CRDT vs Traditional System
&lt;/h1&gt;

&lt;p&gt;Traditional:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Conflicts happen&lt;/li&gt;
&lt;li&gt;Merge problems&lt;/li&gt;
&lt;li&gt;Data can be lost&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;CRDT:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No conflicts&lt;/li&gt;
&lt;li&gt;Automatic merge&lt;/li&gt;
&lt;li&gt;Safer collaboration&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  10. CRDT vs OT (Important Interview Question)
&lt;/h1&gt;

&lt;h2&gt;
  
  
  OT
&lt;/h2&gt;

&lt;p&gt;Operational Transform.&lt;/p&gt;

&lt;p&gt;Old technique.&lt;/p&gt;

&lt;p&gt;Used in older collaborative systems.&lt;/p&gt;




&lt;h2&gt;
  
  
  CRDT
&lt;/h2&gt;

&lt;p&gt;Newer technique.&lt;/p&gt;

&lt;p&gt;Changes merge automatically.&lt;/p&gt;




&lt;h2&gt;
  
  
  Difference:
&lt;/h2&gt;

&lt;p&gt;OT transforms operations.&lt;/p&gt;

&lt;p&gt;CRDT uses special data structures to avoid conflict.&lt;/p&gt;




&lt;h1&gt;
  
  
  11. What problem does CRDT solve?
&lt;/h1&gt;

&lt;p&gt;CRDT solves:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Simultaneous editing&lt;/li&gt;
&lt;li&gt;Merge conflicts&lt;/li&gt;
&lt;li&gt;Offline editing sync&lt;/li&gt;
&lt;li&gt;Distributed data consistency&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Important exam point.&lt;/p&gt;




&lt;h1&gt;
  
  
  12. What is Yjs?
&lt;/h1&gt;

&lt;p&gt;Yjs is a JavaScript library that implements CRDT.&lt;/p&gt;

&lt;p&gt;Simple meaning:&lt;/p&gt;

&lt;p&gt;Yjs gives ready-made CRDT.&lt;/p&gt;

&lt;p&gt;You do not build CRDT from scratch.&lt;/p&gt;




&lt;h1&gt;
  
  
  13. What is this?
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ydoc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Y&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Doc&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates:&lt;/p&gt;

&lt;p&gt;Shared document.&lt;/p&gt;

&lt;p&gt;Like collaborative database in memory.&lt;/p&gt;




&lt;h1&gt;
  
  
  14. What is Y.Text?
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;ydoc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;monaco&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Creates shared text.&lt;/p&gt;

&lt;p&gt;Like collaborative string.&lt;/p&gt;

&lt;p&gt;Used in your code editor project. fileciteturn0file0&lt;/p&gt;




&lt;h1&gt;
  
  
  15. What is y-monaco?
&lt;/h1&gt;

&lt;p&gt;It connects:&lt;/p&gt;

&lt;p&gt;Monaco Editor + Yjs&lt;/p&gt;

&lt;p&gt;Without it:&lt;/p&gt;

&lt;p&gt;Editor changes do not sync.&lt;/p&gt;




&lt;h1&gt;
  
  
  16. What is y-socket.io?
&lt;/h1&gt;

&lt;p&gt;Used for network communication.&lt;/p&gt;

&lt;p&gt;It sends updates between users.&lt;/p&gt;

&lt;p&gt;Without it:&lt;/p&gt;

&lt;p&gt;No real-time collaboration.&lt;/p&gt;




&lt;h1&gt;
  
  
  17. What is Socket.IO?
&lt;/h1&gt;

&lt;p&gt;Socket.IO is real-time communication library.&lt;/p&gt;

&lt;p&gt;Used for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;WebSockets&lt;/li&gt;
&lt;li&gt;Real-time messages&lt;/li&gt;
&lt;li&gt;Live sync&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  18. How your collaborative editor works
&lt;/h1&gt;

&lt;p&gt;Flow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Monaco Editor
↓
Yjs
↓
Socket.IO Provider
↓
Server
↓
Other users
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is your architecture. fileciteturn0file0&lt;/p&gt;




&lt;h1&gt;
  
  
  19. What is awareness?
&lt;/h1&gt;

&lt;p&gt;Awareness means:&lt;/p&gt;

&lt;p&gt;User presence.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Who is online&lt;/li&gt;
&lt;li&gt;Cursor position&lt;/li&gt;
&lt;li&gt;User color&lt;/li&gt;
&lt;li&gt;User name&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;provider&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;awareness&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setLocalStateField&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  20. Awareness vs Document (Interview Question)
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Document
&lt;/h2&gt;

&lt;p&gt;Actual shared content.&lt;/p&gt;

&lt;p&gt;Like:&lt;/p&gt;

&lt;p&gt;Code.&lt;/p&gt;




&lt;h2&gt;
  
  
  Awareness
&lt;/h2&gt;

&lt;p&gt;Temporary user info.&lt;/p&gt;

&lt;p&gt;Like:&lt;/p&gt;

&lt;p&gt;Cursor.&lt;/p&gt;

&lt;p&gt;Difference important.&lt;/p&gt;




&lt;h1&gt;
  
  
  21. What is eventual consistency?
&lt;/h1&gt;

&lt;p&gt;Very important interview question.&lt;/p&gt;

&lt;p&gt;Definition:&lt;/p&gt;

&lt;p&gt;All replicas eventually become consistent.&lt;/p&gt;

&lt;p&gt;Simple:&lt;/p&gt;

&lt;p&gt;Everyone finally sees same data.&lt;/p&gt;




&lt;h1&gt;
  
  
  22. What is a replica?
&lt;/h1&gt;

&lt;p&gt;Replica = copy of data.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;Each user has one copy.&lt;/p&gt;




&lt;h1&gt;
  
  
  23. What is a conflict?
&lt;/h1&gt;

&lt;p&gt;Conflict means:&lt;/p&gt;

&lt;p&gt;Two changes compete.&lt;/p&gt;

&lt;p&gt;CRDT resolves it.&lt;/p&gt;




&lt;h1&gt;
  
  
  24. What is tombstone? (Exam Question)
&lt;/h1&gt;

&lt;p&gt;When data is deleted:&lt;/p&gt;

&lt;p&gt;Sometimes system marks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Deleted = true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;instead of removing immediately.&lt;/p&gt;

&lt;p&gt;This is tombstone.&lt;/p&gt;




&lt;h1&gt;
  
  
  25. Advantages of CRDT
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Conflict-free&lt;/li&gt;
&lt;li&gt;Real-time collaboration&lt;/li&gt;
&lt;li&gt;Offline support&lt;/li&gt;
&lt;li&gt;Automatic merging&lt;/li&gt;
&lt;li&gt;Strong distributed systems support&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Important exam answer.&lt;/p&gt;




&lt;h1&gt;
  
  
  26. Disadvantages of CRDT
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Complex algorithms&lt;/li&gt;
&lt;li&gt;Memory overhead&lt;/li&gt;
&lt;li&gt;Hard to implement&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Common interview question.&lt;/p&gt;




&lt;h1&gt;
  
  
  27. Where CRDT is used
&lt;/h1&gt;

&lt;p&gt;Used in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Collaborative editors&lt;/li&gt;
&lt;li&gt;Multiplayer apps&lt;/li&gt;
&lt;li&gt;Shared whiteboards&lt;/li&gt;
&lt;li&gt;Offline-first apps&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  28. Interview Questions and Answers
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Q1 What is CRDT?
&lt;/h2&gt;

&lt;p&gt;Answer:&lt;/p&gt;

&lt;p&gt;CRDT is a distributed data structure used for conflict-free synchronization.&lt;/p&gt;




&lt;h2&gt;
  
  
  Q2 Why use CRDT?
&lt;/h2&gt;

&lt;p&gt;To solve conflicts in concurrent distributed edits.&lt;/p&gt;




&lt;h2&gt;
  
  
  Q3 Difference between CRDT and OT?
&lt;/h2&gt;

&lt;p&gt;OT transforms operations.&lt;/p&gt;

&lt;p&gt;CRDT avoids conflicts using special data structures.&lt;/p&gt;




&lt;h2&gt;
  
  
  Q4 What is eventual consistency?
&lt;/h2&gt;

&lt;p&gt;All replicas eventually become identical.&lt;/p&gt;




&lt;h2&gt;
  
  
  Q5 What is Yjs?
&lt;/h2&gt;

&lt;p&gt;Yjs is a JavaScript CRDT library.&lt;/p&gt;




&lt;h2&gt;
  
  
  Q6 What is awareness?
&lt;/h2&gt;

&lt;p&gt;Awareness manages user presence.&lt;/p&gt;




&lt;h2&gt;
  
  
  Q7 What is replica?
&lt;/h2&gt;

&lt;p&gt;A copy of shared data.&lt;/p&gt;




&lt;h1&gt;
  
  
  29. Exam Answers (Short)
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Define CRDT.
&lt;/h2&gt;

&lt;p&gt;CRDT is a conflict-free replicated data type used for distributed synchronization.&lt;/p&gt;




&lt;h2&gt;
  
  
  Define replica.
&lt;/h2&gt;

&lt;p&gt;Replica is a copy of shared distributed data.&lt;/p&gt;




&lt;h2&gt;
  
  
  Define convergence.
&lt;/h2&gt;

&lt;p&gt;Convergence means all replicas reach same final state.&lt;/p&gt;




&lt;h2&gt;
  
  
  Define eventual consistency.
&lt;/h2&gt;

&lt;p&gt;All replicas become consistent over time.&lt;/p&gt;




&lt;h1&gt;
  
  
  30. Project Ideas Using CRDT
&lt;/h1&gt;

&lt;p&gt;Beginner:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Collaborative notes app&lt;/li&gt;
&lt;li&gt;Shared todo app&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Intermediate:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Collaborative code editor&lt;/li&gt;
&lt;li&gt;Shared whiteboard&lt;/li&gt;
&lt;li&gt;Multiplayer kanban board&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Advanced:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Figma clone&lt;/li&gt;
&lt;li&gt;Notion clone&lt;/li&gt;
&lt;li&gt;Collaborative IDE&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  31. Viva Questions
&lt;/h1&gt;

&lt;p&gt;Teacher may ask:&lt;/p&gt;

&lt;p&gt;What does CRDT stand for?&lt;/p&gt;

&lt;p&gt;Why use CRDT?&lt;/p&gt;

&lt;p&gt;Difference between CRDT and OT?&lt;/p&gt;

&lt;p&gt;What is Yjs?&lt;/p&gt;

&lt;p&gt;What is awareness?&lt;/p&gt;

&lt;p&gt;Prepare these.&lt;/p&gt;




&lt;h1&gt;
  
  
  32. Remember Formula
&lt;/h1&gt;

&lt;p&gt;Think:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CRDT =
Multiple users
+
No conflicts
+
Automatic merge
+
Same final data
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Easy memory trick.&lt;/p&gt;




&lt;h1&gt;
  
  
  33. Final Easy Definition
&lt;/h1&gt;

&lt;p&gt;CRDT is a special distributed data structure that lets many users edit the same data at the same time without conflicts, while automatically keeping all copies synchronized.&lt;/p&gt;

&lt;p&gt;This is correct for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Exams&lt;/li&gt;
&lt;li&gt;Interviews&lt;/li&gt;
&lt;li&gt;Viva&lt;/li&gt;
&lt;li&gt;Projects&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Done.&lt;/p&gt;




&lt;h1&gt;
  
  
  34. Complete Topic Coverage (Added Missing Topics)
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Characteristics of CRDT
&lt;/h2&gt;

&lt;p&gt;A good CRDT has:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Convergence&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;All replicas reach same state.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Commutativity&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Order does not matter.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A then B = B then A
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Idempotence&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Applying same update twice gives same result.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Associativity&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Grouping operations does not change result.&lt;/p&gt;

&lt;p&gt;These are important interview topics.&lt;/p&gt;




&lt;h1&gt;
  
  
  35. Diagram: CRDT Merge
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;         User A
          |
       Insert X
          |
          |
          v
      -----------
      CRDT Merge Engine
      -----------
          ^
          |
       Insert Y
          |
         User B

Final:
X and Y both preserved
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  36. Example of Conflict Without CRDT
&lt;/h1&gt;

&lt;p&gt;User A edits:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Apple
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;User B edits:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Apple Mango
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Traditional system:&lt;/p&gt;

&lt;p&gt;One may overwrite other.&lt;/p&gt;

&lt;p&gt;Data loss possible.&lt;/p&gt;




&lt;h1&gt;
  
  
  37. Example With CRDT
&lt;/h1&gt;

&lt;p&gt;Both changes preserved.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Apple Mango
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No loss.&lt;/p&gt;




&lt;h1&gt;
  
  
  38. Types of Shared Yjs Data Types
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Y.Text
&lt;/h2&gt;

&lt;p&gt;Collaborative text.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;Shared code editor.&lt;/p&gt;




&lt;h2&gt;
  
  
  Y.Array
&lt;/h2&gt;

&lt;p&gt;Shared arrays.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Task1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Task2&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Y.Map
&lt;/h2&gt;

&lt;p&gt;Shared object.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Abhishek&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;editor&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Y.XmlFragment
&lt;/h2&gt;

&lt;p&gt;Rich text editors.&lt;/p&gt;




&lt;h1&gt;
  
  
  39. Diagram: Yjs Architecture
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User Types
   |
Monaco Editor
   |
y-monaco
   |
Yjs Document
   |
y-socket.io
   |
Server
   |
Other Clients
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  40. Example: Y.Map
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;ydoc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;users&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Abhishek&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Shared map updated.&lt;/p&gt;




&lt;h1&gt;
  
  
  41. Example: Y.Array
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;tasks&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;ydoc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;tasks&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;tasks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Learn CRDT&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Collaborative shared array.&lt;/p&gt;




&lt;h1&gt;
  
  
  42. Example: Awareness
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;provider&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;awareness&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setLocalStateField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;user&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Abhishek&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blue&lt;/span&gt;&lt;span class="dl"&gt;'&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;p&gt;Stores presence.&lt;/p&gt;




&lt;h1&gt;
  
  
  43. Interview Question
&lt;/h1&gt;

&lt;p&gt;Difference between Y.Doc and Y.Text?&lt;/p&gt;

&lt;p&gt;Answer:&lt;/p&gt;

&lt;p&gt;Y.Doc is complete shared document.&lt;/p&gt;

&lt;p&gt;Y.Text is collaborative text inside Y.Doc.&lt;/p&gt;




&lt;h1&gt;
  
  
  44. What is WebSocket?
&lt;/h1&gt;

&lt;p&gt;Interview favorite.&lt;/p&gt;

&lt;p&gt;WebSocket:&lt;/p&gt;

&lt;p&gt;Two-way real-time communication protocol.&lt;/p&gt;

&lt;p&gt;Used for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Chat&lt;/li&gt;
&lt;li&gt;Collaboration&lt;/li&gt;
&lt;li&gt;Live updates&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  45. WebSocket vs HTTP
&lt;/h1&gt;

&lt;p&gt;HTTP:&lt;/p&gt;

&lt;p&gt;Request-response.&lt;/p&gt;

&lt;p&gt;One-way.&lt;/p&gt;




&lt;p&gt;WebSocket:&lt;/p&gt;

&lt;p&gt;Persistent connection.&lt;/p&gt;

&lt;p&gt;Two-way.&lt;/p&gt;

&lt;p&gt;Better for real-time.&lt;/p&gt;




&lt;h1&gt;
  
  
  46. Diagram
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HTTP
Client -&amp;gt; Server

WebSocket
Client &amp;lt;-&amp;gt; Server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  47. What is Socket.IO Provider?
&lt;/h1&gt;

&lt;p&gt;Bridge between:&lt;/p&gt;

&lt;p&gt;Yjs and Socket.IO.&lt;/p&gt;

&lt;p&gt;Responsible for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;sending updates&lt;/li&gt;
&lt;li&gt;receiving updates&lt;/li&gt;
&lt;li&gt;syncing replicas&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  48. What is Replica Synchronization?
&lt;/h1&gt;

&lt;p&gt;Keeping all copies updated.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;User A copy&lt;br&gt;
User B copy&lt;br&gt;
Server copy&lt;/p&gt;

&lt;p&gt;All sync.&lt;/p&gt;


&lt;h1&gt;
  
  
  49. Example of Concurrent Editing
&lt;/h1&gt;

&lt;p&gt;A types:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cat
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;B types:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dog
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;CRDT merges.&lt;/p&gt;

&lt;p&gt;Final:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cat dog
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  50. What is Offline Editing?
&lt;/h1&gt;

&lt;p&gt;User edits without internet.&lt;/p&gt;

&lt;p&gt;Later reconnects.&lt;/p&gt;

&lt;p&gt;Changes merge automatically.&lt;/p&gt;

&lt;p&gt;Huge CRDT feature.&lt;/p&gt;




&lt;h1&gt;
  
  
  51. What is Causal Ordering?
&lt;/h1&gt;

&lt;p&gt;Advanced interview question.&lt;/p&gt;

&lt;p&gt;Operations applied in logical order.&lt;/p&gt;

&lt;p&gt;Cause before effect.&lt;/p&gt;




&lt;h1&gt;
  
  
  52. Lamport Clock (Simple)
&lt;/h1&gt;

&lt;p&gt;Used to track event ordering.&lt;/p&gt;

&lt;p&gt;Important distributed systems topic.&lt;/p&gt;




&lt;h1&gt;
  
  
  53. Vector Clock
&lt;/h1&gt;

&lt;p&gt;Tracks ordering across multiple machines.&lt;/p&gt;

&lt;p&gt;Advanced topic.&lt;/p&gt;




&lt;h1&gt;
  
  
  54. Strong Eventual Consistency
&lt;/h1&gt;

&lt;p&gt;All replicas:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;receive same updates&lt;/li&gt;
&lt;li&gt;reach same state&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Important theory topic.&lt;/p&gt;




&lt;h1&gt;
  
  
  55. State-Based Example
&lt;/h1&gt;

&lt;p&gt;Send:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Whole document
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  56. Operation-Based Example
&lt;/h1&gt;

&lt;p&gt;Send:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Insert A
Delete B
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  57. Difference
&lt;/h1&gt;

&lt;p&gt;State-based:&lt;/p&gt;

&lt;p&gt;Sends state.&lt;/p&gt;

&lt;p&gt;Operation-based:&lt;/p&gt;

&lt;p&gt;Sends operations.&lt;/p&gt;

&lt;p&gt;Common exam question.&lt;/p&gt;




&lt;h1&gt;
  
  
  58. What is Distributed System?
&lt;/h1&gt;

&lt;p&gt;Very important prerequisite.&lt;/p&gt;

&lt;p&gt;Distributed system:&lt;/p&gt;

&lt;p&gt;Multiple computers working together.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Google&lt;/li&gt;
&lt;li&gt;AWS&lt;/li&gt;
&lt;li&gt;Collaborative editors&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  59. CRDT in Distributed Systems
&lt;/h1&gt;

&lt;p&gt;CRDT helps distributed systems maintain consistency.&lt;/p&gt;




&lt;h1&gt;
  
  
  60. What is Consistency?
&lt;/h1&gt;

&lt;p&gt;Consistency means:&lt;/p&gt;

&lt;p&gt;All users see correct data.&lt;/p&gt;




&lt;h1&gt;
  
  
  61. What is Data Synchronization?
&lt;/h1&gt;

&lt;p&gt;Keeping multiple copies updated.&lt;/p&gt;




&lt;h1&gt;
  
  
  62. What is Automatic Merge?
&lt;/h1&gt;

&lt;p&gt;Merging changes without manual work.&lt;/p&gt;

&lt;p&gt;CRDT does this.&lt;/p&gt;




&lt;h1&gt;
  
  
  63. Traditional Locking vs CRDT
&lt;/h1&gt;

&lt;p&gt;Locking:&lt;/p&gt;

&lt;p&gt;Only one user edits.&lt;/p&gt;




&lt;p&gt;CRDT:&lt;/p&gt;

&lt;p&gt;Many users edit.&lt;/p&gt;

&lt;p&gt;Much better collaboration.&lt;/p&gt;




&lt;h1&gt;
  
  
  64. Diagram
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Locking:
User A edits
User B waits

CRDT:
User A edits
User B edits
Both succeed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  65. Common Viva Questions
&lt;/h1&gt;

&lt;p&gt;What is convergence?&lt;/p&gt;

&lt;p&gt;What is commutativity?&lt;/p&gt;

&lt;p&gt;What is idempotence?&lt;/p&gt;

&lt;p&gt;What is awareness?&lt;/p&gt;

&lt;p&gt;What is Y.Doc?&lt;/p&gt;

&lt;p&gt;What is Y.Text?&lt;/p&gt;

&lt;p&gt;What is Socket.IO?&lt;/p&gt;

&lt;p&gt;What is WebSocket?&lt;/p&gt;

&lt;p&gt;What is eventual consistency?&lt;/p&gt;

&lt;p&gt;Important.&lt;/p&gt;




&lt;h1&gt;
  
  
  66. Common Mistakes Students Make
&lt;/h1&gt;

&lt;p&gt;Confusing:&lt;/p&gt;

&lt;p&gt;WebSocket with CRDT.&lt;/p&gt;

&lt;p&gt;Wrong.&lt;/p&gt;

&lt;p&gt;WebSocket = transport.&lt;/p&gt;

&lt;p&gt;CRDT = data structure.&lt;/p&gt;

&lt;p&gt;Very different.&lt;/p&gt;




&lt;h1&gt;
  
  
  67. Common Mistake
&lt;/h1&gt;

&lt;p&gt;Thinking Yjs is WebSocket.&lt;/p&gt;

&lt;p&gt;Wrong.&lt;/p&gt;

&lt;p&gt;Yjs = CRDT engine.&lt;/p&gt;

&lt;p&gt;Socket.IO = transport.&lt;/p&gt;




&lt;h1&gt;
  
  
  68. Interview Question
&lt;/h1&gt;

&lt;p&gt;Why not use only WebSocket?&lt;/p&gt;

&lt;p&gt;Answer:&lt;/p&gt;

&lt;p&gt;WebSocket only sends messages.&lt;/p&gt;

&lt;p&gt;It does not solve conflicts.&lt;/p&gt;

&lt;p&gt;CRDT solves conflicts.&lt;/p&gt;

&lt;p&gt;Very important answer.&lt;/p&gt;




&lt;h1&gt;
  
  
  69. Interview Question
&lt;/h1&gt;

&lt;p&gt;Why not use only database?&lt;/p&gt;

&lt;p&gt;Answer:&lt;/p&gt;

&lt;p&gt;Database stores data.&lt;/p&gt;

&lt;p&gt;CRDT handles distributed concurrent editing.&lt;/p&gt;

&lt;p&gt;Different purpose.&lt;/p&gt;




&lt;h1&gt;
  
  
  70. Exam Question
&lt;/h1&gt;

&lt;p&gt;Advantages of Yjs&lt;/p&gt;

&lt;p&gt;Answer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fast&lt;/li&gt;
&lt;li&gt;Conflict-free&lt;/li&gt;
&lt;li&gt;Real-time sync&lt;/li&gt;
&lt;li&gt;Offline support&lt;/li&gt;
&lt;li&gt;Efficient updates&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  71. Disadvantages of Yjs
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Learning curve&lt;/li&gt;
&lt;li&gt;Complex internals&lt;/li&gt;
&lt;li&gt;Large documents need optimization&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  72. Diagram: Packages Role
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yjs
↓
CRDT Engine


y-monaco
↓
Editor Binding


y-socket.io
↓
Network Sync
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  73. Example Interview Answer
&lt;/h1&gt;

&lt;p&gt;Q:&lt;br&gt;
How does collaborative editor work?&lt;/p&gt;

&lt;p&gt;Answer:&lt;/p&gt;

&lt;p&gt;Monaco captures edits.&lt;/p&gt;

&lt;p&gt;Yjs converts edits into CRDT updates.&lt;/p&gt;

&lt;p&gt;Socket.IO sends updates.&lt;/p&gt;

&lt;p&gt;Other clients receive and apply updates.&lt;/p&gt;

&lt;p&gt;All replicas converge.&lt;/p&gt;

&lt;p&gt;Excellent answer.&lt;/p&gt;




&lt;h1&gt;
  
  
  74. Scenario Question
&lt;/h1&gt;

&lt;p&gt;What happens if two users type at same time?&lt;/p&gt;

&lt;p&gt;Answer:&lt;/p&gt;

&lt;p&gt;CRDT merges concurrent changes automatically.&lt;/p&gt;

&lt;p&gt;No data loss.&lt;/p&gt;




&lt;h1&gt;
  
  
  75. Scenario Question
&lt;/h1&gt;

&lt;p&gt;What if user disconnects?&lt;/p&gt;

&lt;p&gt;Answer:&lt;/p&gt;

&lt;p&gt;User can reconnect.&lt;/p&gt;

&lt;p&gt;CRDT can merge updates.&lt;/p&gt;




&lt;h1&gt;
  
  
  76. Important Terms Dictionary
&lt;/h1&gt;

&lt;p&gt;Conflict&lt;/p&gt;

&lt;p&gt;Replica&lt;/p&gt;

&lt;p&gt;Convergence&lt;/p&gt;

&lt;p&gt;Consistency&lt;/p&gt;

&lt;p&gt;Synchronization&lt;/p&gt;

&lt;p&gt;Awareness&lt;/p&gt;

&lt;p&gt;Tombstone&lt;/p&gt;

&lt;p&gt;Operation&lt;/p&gt;

&lt;p&gt;State&lt;/p&gt;

&lt;p&gt;Causality&lt;/p&gt;

&lt;p&gt;Learn all.&lt;/p&gt;




&lt;h1&gt;
  
  
  77. Big Diagram Full System
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User A
 |
Monaco
 |
Yjs
 |
Socket.IO
 |
Server
 |
Socket.IO
 |
Yjs
 |
Monaco
 |
User B
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Full architecture.&lt;/p&gt;




&lt;h1&gt;
  
  
  78. Practice Questions
&lt;/h1&gt;

&lt;p&gt;Explain CRDT.&lt;/p&gt;

&lt;p&gt;Explain Yjs.&lt;/p&gt;

&lt;p&gt;Explain Awareness.&lt;/p&gt;

&lt;p&gt;Explain convergence.&lt;/p&gt;

&lt;p&gt;Explain eventual consistency.&lt;/p&gt;

&lt;p&gt;Explain state-based CRDT.&lt;/p&gt;

&lt;p&gt;Explain operation-based CRDT.&lt;/p&gt;

&lt;p&gt;Explain WebSocket vs HTTP.&lt;/p&gt;

&lt;p&gt;Practice these.&lt;/p&gt;




&lt;h1&gt;
  
  
  79. 2 Mark Questions
&lt;/h1&gt;

&lt;p&gt;Define CRDT.&lt;/p&gt;

&lt;p&gt;Define replica.&lt;/p&gt;

&lt;p&gt;Define convergence.&lt;/p&gt;

&lt;p&gt;Define awareness.&lt;/p&gt;




&lt;h1&gt;
  
  
  80. 5 Mark Questions
&lt;/h1&gt;

&lt;p&gt;Explain CRDT architecture.&lt;/p&gt;

&lt;p&gt;Explain Yjs packages.&lt;/p&gt;

&lt;p&gt;Difference between CRDT and OT.&lt;/p&gt;




&lt;h1&gt;
  
  
  81. 10 Mark Questions
&lt;/h1&gt;

&lt;p&gt;Explain conflict-free replicated data types with diagram.&lt;/p&gt;

&lt;p&gt;Explain collaborative editor architecture using Yjs and Socket.IO.&lt;/p&gt;

&lt;p&gt;Very important.&lt;/p&gt;




&lt;h1&gt;
  
  
  82. Advanced Topics To Mention In Interview
&lt;/h1&gt;

&lt;p&gt;Lamport Clock&lt;/p&gt;

&lt;p&gt;Vector Clock&lt;/p&gt;

&lt;p&gt;Tombstones&lt;/p&gt;

&lt;p&gt;Causal Ordering&lt;/p&gt;

&lt;p&gt;Strong Eventual Consistency&lt;/p&gt;

&lt;p&gt;Mentioning these makes you look strong.&lt;/p&gt;




&lt;h1&gt;
  
  
  83. Final Memory Map
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CRDT
├── Conflict-free
├── Replicated
├── Data Type
│
├── Convergence
├── Consistency
├── Replicas
│
├── Yjs
│   ├── Y.Doc
│   ├── Y.Text
│   ├── Y.Map
│   └── Y.Array
│
├── y-monaco
├── y-socket.io
│
├── WebSocket
├── Awareness
│
└── Collaborative Editors
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Complete map.&lt;/p&gt;




&lt;h1&gt;
  
  
  84. Final Interview Definition (Best)
&lt;/h1&gt;

&lt;p&gt;CRDT is a distributed data structure that allows multiple users to modify shared replicated data concurrently while automatically resolving conflicts and ensuring all replicas converge to the same final state.&lt;/p&gt;

&lt;p&gt;Use this in interviews.&lt;/p&gt;

&lt;p&gt;Now coverage is much more complete.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>🚀 Add Element at the End of a Linked List (0 Monster Level)</title>
      <dc:creator>Abhishek Gupta </dc:creator>
      <pubDate>Sat, 18 Apr 2026 08:46:58 +0000</pubDate>
      <link>https://forem.com/abhishek-nexgen-dev/add-element-at-the-end-of-a-linked-list-0-monster-level-4e4h</link>
      <guid>https://forem.com/abhishek-nexgen-dev/add-element-at-the-end-of-a-linked-list-0-monster-level-4e4h</guid>
      <description>&lt;h1&gt;
  
  
  🧱 1. What is a Linked List?
&lt;/h1&gt;

&lt;p&gt;A Linked List is a collection of &lt;strong&gt;nodes connected like a chain&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Each node contains:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;value&lt;/code&gt; → data&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;next&lt;/code&gt; → reference (link) to the next node
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[value | next] → [value | next] → [value | null]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;👉 The last node always points to &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  🧠 2. Core Idea (Very Important)
&lt;/h1&gt;

&lt;p&gt;To add a node at the end:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Start from head → move step by step → find last node → connect new node”&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  🟢 3. Empty List Case (MOST IMPORTANT START)
&lt;/h1&gt;

&lt;p&gt;If the list is empty:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;head = null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Add 5:
&lt;/h3&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%2Fqqc8jrv01edi5x5g089y.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%2Fqqc8jrv01edi5x5g089y.png" alt=" " width="322" height="231"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We create:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;newNode = { value: 5, next: null }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;head → 5 → null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✔ First node becomes the head&lt;/p&gt;




&lt;h1&gt;
  
  
  🟡 4. First Insertion Example
&lt;/h1&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%2F1uvdoj55twjlui7vrusn.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%2F1uvdoj55twjlui7vrusn.png" alt=" " width="322" height="231"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Insert: 5
&lt;/h2&gt;



&lt;p&gt;```text id="a5"&lt;br&gt;
head → null&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;


After push(5):



```text
head → 5 → null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;✔ Head is updated&lt;/p&gt;


&lt;h2&gt;
  
  
  Insert: 10
&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%2Fe73y6jf5ag6oh21t7dwp.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%2Fe73y6jf5ag6oh21t7dwp.png" alt=" " width="322" height="231"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Start:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;5 → null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Step 1: Create node
&lt;/h3&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%2Fj8y7l9n8uenlmzut2ivg.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%2Fj8y7l9n8uenlmzut2ivg.png" alt=" " width="539" height="488"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10 → null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Step 2: Traverse from head
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;current = 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Check:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;5.next === null ❌ stop here
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Step 3: Attach node
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;5.next = 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;5 → 10 → null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  🔵 5. Second Insertion (DETAILED DRY RUN)
&lt;/h1&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%2F8134gt7t8u9lbra0z8z0.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%2F8134gt7t8u9lbra0z8z0.png" alt=" " width="790" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Insert: 20
&lt;/h2&gt;

&lt;p&gt;Start:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;5 → 10 → null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Step 1: Create node
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;20 → null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Step 2: Start from head
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;current = 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Step 3: Move step-by-step
&lt;/h2&gt;

&lt;h3&gt;
  
  
  First move:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;5 → 10 → null
   ↑ current moves
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;current = 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Step 4: Check next
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10.next === null ✔ STOP
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;👉 We found last node&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 5: Attach new node
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10.next = 20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Final result:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;5 → 10 → 20 → null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  💻 6. Final Clean Code
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LinkedList&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Case 1: empty list&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Case 2: traverse to last node&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// attach new node&lt;/span&gt;
    &lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt;&lt;span class="p"&gt;;&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;h1&gt;
  
  
  ⚠️ 7. VERY IMPORTANT RULES
&lt;/h1&gt;

&lt;p&gt;✔ Always check &lt;code&gt;this.head&lt;/code&gt; (NOT newNode)&lt;br&gt;
✔ Never do &lt;code&gt;current = newNode&lt;/code&gt; ❌&lt;br&gt;
✔ Always use &lt;code&gt;current.next = newNode&lt;/code&gt; ✅&lt;br&gt;
✔ Use &lt;code&gt;while (current.next)&lt;/code&gt; for traversal&lt;/p&gt;




&lt;h1&gt;
  
  
  ⏱️ 8. Complexity
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Without tail: &lt;strong&gt;O(n)&lt;/strong&gt; (you travel whole list)&lt;/li&gt;
&lt;li&gt;With tail pointer: &lt;strong&gt;O(1)&lt;/strong&gt; (instant insert)&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  🔥 9. Interview-Level Insight
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;Linked list insertion at end is not about “adding value”&lt;br&gt;
It is about “changing references”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You are NOT storing memory addresses manually—&lt;br&gt;
JavaScript automatically stores &lt;strong&gt;object references&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  🧠 Final Mental Model
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;“Walk until null → replace null with new node”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;OR&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Find last node → connect next pointer”&lt;/p&gt;
&lt;/blockquote&gt;

</description>
    </item>
    <item>
      <title>Docker: From Zero to Production (Complete Guide for Developers)</title>
      <dc:creator>Abhishek Gupta </dc:creator>
      <pubDate>Wed, 15 Apr 2026 14:12:08 +0000</pubDate>
      <link>https://forem.com/abhishek-nexgen-dev/docker-from-zero-to-production-complete-guide-for-developers-3m6e</link>
      <guid>https://forem.com/abhishek-nexgen-dev/docker-from-zero-to-production-complete-guide-for-developers-3m6e</guid>
      <description>&lt;h1&gt;
  
  
  🚧 Problem Before Docker
&lt;/h1&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%2Fehyjbgq94fl1cyn6yv4b.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%2Fehyjbgq94fl1cyn6yv4b.png" alt=" " width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As a developer, you often face a very common and frustrating problem:&lt;br&gt;&lt;br&gt;
your code works perfectly on your system but fails on someone else’s.&lt;/p&gt;

&lt;p&gt;It may run fine on Windows but break on Linux, or work on your laptop but not on your teammate’s machine. This happens because every system has a different environment.&lt;/p&gt;




&lt;h1&gt;
  
  
  🎯 Key Problems
&lt;/h1&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%2F87463j8mae1cjnij6q1o.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%2F87463j8mae1cjnij6q1o.png" alt=" " width="800" height="394"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Environment Inconsistency
&lt;/h2&gt;

&lt;p&gt;Different developers used different setups:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Different operating systems
&lt;/li&gt;
&lt;li&gt;Different runtime versions
&lt;/li&gt;
&lt;li&gt;Different configurations
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 Result: Same code behaves differently on different machines&lt;/p&gt;




&lt;h2&gt;
  
  
  2. “It Works on My Machine” Problem
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Code works on developer’s system
&lt;/li&gt;
&lt;li&gt;Fails on tester’s or production system
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 Root Cause: Environment mismatch, not code issues  &lt;/p&gt;




&lt;h2&gt;
  
  
  3. Operating System Differences
&lt;/h2&gt;

&lt;p&gt;Different OS behave differently:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Windows
&lt;/li&gt;
&lt;li&gt;Linux
&lt;/li&gt;
&lt;li&gt;macOS
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Common Issues:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;File system differences
&lt;/li&gt;
&lt;li&gt;Path handling differences
&lt;/li&gt;
&lt;li&gt;OS-specific dependencies
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 Result: Code becomes OS-dependent&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Dependency &amp;amp; Version Conflicts
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Different versions of libraries installed
&lt;/li&gt;
&lt;li&gt;Dependencies may not be compatible
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 Result:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Unexpected errors
&lt;/li&gt;
&lt;li&gt;Application crashes
&lt;/li&gt;
&lt;li&gt;Hard-to-debug issues
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  5. Difficult Project Setup
&lt;/h2&gt;

&lt;p&gt;To run a project, developers needed to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install runtime (Node, Python, etc.)
&lt;/li&gt;
&lt;li&gt;Install databases
&lt;/li&gt;
&lt;li&gt;Configure environment variables
&lt;/li&gt;
&lt;li&gt;Install project dependencies
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 Result:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Time-consuming setup
&lt;/li&gt;
&lt;li&gt;High chance of mistakes
&lt;/li&gt;
&lt;li&gt;Difficult for beginners
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  6. Development vs Production Mismatch
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Development environment ≠ Production environment
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 Example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dev → Windows
&lt;/li&gt;
&lt;li&gt;Prod → Linux
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 Result:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Works locally
&lt;/li&gt;
&lt;li&gt;Fails after deployment
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  7. Unreliable Testing
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Bugs cannot be reproduced easily
&lt;/li&gt;
&lt;li&gt;Different outputs on different systems
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 Reason: Testing environment is not consistent  &lt;/p&gt;




&lt;h2&gt;
  
  
  8. Lack of Standardization
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Every developer uses their own setup
&lt;/li&gt;
&lt;li&gt;No shared or fixed environment
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 Result:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Integration issues
&lt;/li&gt;
&lt;li&gt;Slower team collaboration
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  9. Hidden System Dependencies
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Applications depend on system-level libraries
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 If missing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Application fails to start
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  10. Manual Environment Management
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Everything is installed and managed manually
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 Result:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Error-prone process
&lt;/li&gt;
&lt;li&gt;Wastes time and effort
&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  🏢 In Companies (Imagine This Scenario)
&lt;/h1&gt;

&lt;p&gt;Imagine a company with hundreds or even thousands of developers working on the same project.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Some use Windows
&lt;/li&gt;
&lt;li&gt;Some use Linux
&lt;/li&gt;
&lt;li&gt;Some use macOS
&lt;/li&gt;
&lt;li&gt;Everyone has different versions of tools, runtimes, and dependencies
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now think what actually happens:&lt;/p&gt;

&lt;p&gt;👉 One developer writes code and it works perfectly on their system&lt;br&gt;&lt;br&gt;
👉 Another developer pulls the same code and gets errors&lt;br&gt;&lt;br&gt;
👉 The tester reports bugs that the developer cannot reproduce&lt;br&gt;&lt;br&gt;
👉 The DevOps team faces issues during deployment&lt;br&gt;&lt;br&gt;
👉 The same build behaves differently in staging and production  &lt;/p&gt;




&lt;h3&gt;
  
  
  🔥 Real Problems in Teams
&lt;/h3&gt;

&lt;h4&gt;
  
  
  ⏱️ Time Wasted
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Developers spend hours fixing setup issues instead of writing code
&lt;/li&gt;
&lt;li&gt;Debugging environment problems instead of real bugs
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  🔁 Constant Back-and-Forth
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;“It’s working on my machine” vs “It’s broken here”
&lt;/li&gt;
&lt;li&gt;Developers, testers, and DevOps keep blaming each other
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  🧑‍💻 Difficult Onboarding
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;New developers take days (or weeks) to set up the project
&lt;/li&gt;
&lt;li&gt;Need to follow long setup docs that may already be outdated
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  🚀 Delayed Releases
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Builds fail unexpectedly
&lt;/li&gt;
&lt;li&gt;Deployment issues delay product releases
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  🐞 Hard-to-Fix Bugs
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Bugs cannot be reproduced easily
&lt;/li&gt;
&lt;li&gt;Same code gives different outputs in different environments
&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  💡 2. Birth of Docker (Complete + Beginner Friendly + Exam Ready)
&lt;/h1&gt;

&lt;p&gt;Docker was originally developed as an internal tool at dotCloud to manage application environments efficiently. Developers faced issues like dependency conflicts, environment differences, and unreliable deployments.&lt;/p&gt;

&lt;p&gt;Docker solves these problems using containerization, where applications and their dependencies are packaged into containers. These containers run consistently across development, testing, and production environments.&lt;/p&gt;

&lt;p&gt;Unlike virtual machines, Docker containers are lightweight and share the host operating system, making them faster and more efficient.&lt;/p&gt;

&lt;h2&gt;
  
  
  🏢 The Story Behind Docker (Simple &amp;amp; Real)
&lt;/h2&gt;

&lt;p&gt;Before Docker, developers faced one of the most frustrating problems:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;❌ “It works on my machine, but not on yours.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A company called &lt;strong&gt;dotCloud&lt;/strong&gt; was building a platform to help developers deploy applications easily.&lt;/p&gt;

&lt;p&gt;But they ran into serious challenges:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Every application needed a different environment
&lt;/li&gt;
&lt;li&gt;Managing dependencies was complex
&lt;/li&gt;
&lt;li&gt;Deployments were failing
&lt;/li&gt;
&lt;li&gt;Scaling applications was difficult
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 The real problem was not the code — it was the &lt;strong&gt;environment&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So they created an internal tool to fix this problem.&lt;/p&gt;

&lt;p&gt;👉 That internal tool later became &lt;strong&gt;Docker&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  👨‍💻 Who Created Docker
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Creator 👉 &lt;strong&gt;Solomon Hykes&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Company 👉 &lt;strong&gt;dotCloud (later Docker Inc.)&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Year 👉 &lt;strong&gt;2013&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 Initially built for internal use&lt;br&gt;&lt;br&gt;
👉 Later released as an &lt;strong&gt;open-source project&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  📜 Short History (Easy Timeline)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;🔹 Before 2013 → Environment issues everywhere
&lt;/li&gt;
&lt;li&gt;🔹 2013 → Docker launched as open-source
&lt;/li&gt;
&lt;li&gt;🔹 2014+ → Rapid adoption by developers and companies
&lt;/li&gt;
&lt;li&gt;🔹 Today → Industry standard for development &amp;amp; DevOps
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🎯 Main Goal of Docker
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;✅ “Run the same application, the same way, everywhere.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Also known as:&lt;/p&gt;

&lt;p&gt;👉 &lt;strong&gt;Build once, run anywhere&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  🧠 Core Idea (Very Important)
&lt;/h2&gt;

&lt;p&gt;Docker introduced a powerful but simple idea:&lt;/p&gt;

&lt;p&gt;👉 Instead of setting up everything manually,&lt;br&gt;&lt;br&gt;
👉 &lt;strong&gt;package everything into one unit&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This unit includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Application code
&lt;/li&gt;
&lt;li&gt;Runtime (Node.js, Python, etc.)
&lt;/li&gt;
&lt;li&gt;Libraries
&lt;/li&gt;
&lt;li&gt;Dependencies
&lt;/li&gt;
&lt;li&gt;Configuration
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 This unit is called a &lt;strong&gt;container&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  📦 What is a Container (Simple Words)
&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%2Fjquuu4qw5m289k7da75d.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%2Fjquuu4qw5m289k7da75d.png" alt=" " width="800" height="383"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A container is:&lt;/p&gt;

&lt;p&gt;👉 A lightweight package that contains everything needed to run an application&lt;/p&gt;

&lt;p&gt;Think of it like:&lt;/p&gt;

&lt;p&gt;🎒 “Your app + all its requirements in one bag”&lt;/p&gt;

&lt;p&gt;Wherever you run this container → it behaves the same way&lt;/p&gt;




&lt;h2&gt;
  
  
  🔥 How Docker Solves Developer Problems
&lt;/h2&gt;

&lt;h3&gt;
  
  
  ✅ 1. Same Environment Everywhere
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Works on Windows, Linux, macOS
&lt;/li&gt;
&lt;li&gt;Eliminates environment differences
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  ✅ 2. No Version Conflicts
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Dependencies are fixed inside the container
&lt;/li&gt;
&lt;li&gt;No mismatch between developers
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  ✅ 3. Easy Project Setup
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;No need for manual installation
&lt;/li&gt;
&lt;li&gt;Run a single command → project starts
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  ✅ 4. No “Works on My Machine” Problem
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Same container = same result everywhere
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  ✅ 5. Reliable Deployment
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;What works in development → works in production
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  ✅ 6. Faster Team Collaboration
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Everyone uses the same environment
&lt;/li&gt;
&lt;li&gt;Easy onboarding for new developers
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  ✅ 7. Isolation
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Each app runs in its own container
&lt;/li&gt;
&lt;li&gt;No conflicts between projects
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ⚖️ Virtualization vs Containerization
&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%2Fsudlw0eo5o8poiytr70a.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%2Fsudlw0eo5o8poiytr70a.png" alt=" " width="800" height="1003"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  🖥️ Virtual Machine (VM)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Each VM has a full operating system
&lt;/li&gt;
&lt;li&gt;Heavy and slower
&lt;/li&gt;
&lt;li&gt;Uses more CPU and RAM
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 Examples: VirtualBox, VMware  &lt;/p&gt;




&lt;h3&gt;
  
  
  📦 Docker (Container)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Shares the host OS kernel
&lt;/li&gt;
&lt;li&gt;Lightweight and fast
&lt;/li&gt;
&lt;li&gt;Uses fewer resources
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 Example: Docker  &lt;/p&gt;

&lt;h2&gt;
  
  
  ❓ Is Docker a Virtual Machine?
&lt;/h2&gt;

&lt;p&gt;👉 ❌ No  &lt;/p&gt;

&lt;p&gt;But:&lt;/p&gt;

&lt;p&gt;👉 ✅ Both provide isolation  &lt;/p&gt;

&lt;p&gt;Difference:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;VM → Full OS
&lt;/li&gt;
&lt;li&gt;Docker → Lightweight container
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Docker was created in 2013 by Solomon Hykes at dotCloud to solve environment inconsistency problems. It uses containerization to package applications along with their dependencies, ensuring they run consistently across different systems.&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%2Fp3z7sm81tb14rfd4ry0o.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%2Fp3z7sm81tb14rfd4ry0o.png" alt=" " width="800" height="411"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>🔗 Linked List: 0 100% Mastery Roadmap</title>
      <dc:creator>Abhishek Gupta </dc:creator>
      <pubDate>Sat, 11 Apr 2026 15:42:29 +0000</pubDate>
      <link>https://forem.com/abhishek-nexgen-dev/linked-list-0-100-mastery-roadmap-26l</link>
      <guid>https://forem.com/abhishek-nexgen-dev/linked-list-0-100-mastery-roadmap-26l</guid>
      <description>&lt;h1&gt;
  
  
  🟢 1. WHAT IS A LINKED LIST?
&lt;/h1&gt;

&lt;p&gt;A &lt;strong&gt;Linked List&lt;/strong&gt; is a linear data structure in which elements are stored in the form of &lt;strong&gt;nodes&lt;/strong&gt;, and each node is connected to the next node using a &lt;strong&gt;pointer&lt;/strong&gt; (reference).&lt;/p&gt;

&lt;p&gt;Unlike arrays, elements in a linked list are &lt;strong&gt;not stored in contiguous memory locations&lt;/strong&gt;. Instead, they are stored at &lt;strong&gt;different (non-contiguous) memory locations&lt;/strong&gt; and are connected using &lt;strong&gt;pointers (or references)&lt;/strong&gt;.&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%2Fcebkbf1co5ihatu4q6zf.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%2Fcebkbf1co5ihatu4q6zf.png" alt=" "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🔹 Basic Concept (Easy Words)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A linked list is made up of &lt;strong&gt;nodes (small blocks of data)&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Each node contains:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Data&lt;/strong&gt; → stores the value
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Next (Pointer)&lt;/strong&gt; → stores the address of the next node
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Nodes are connected using &lt;strong&gt;pointers&lt;/strong&gt;, forming a &lt;strong&gt;chain-like structure&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Nodes are stored in &lt;strong&gt;different (non-contiguous) memory locations&lt;/strong&gt;&lt;/p&gt;&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%2Fp2mjjvn0m2abc6sk4jxd.jpg" 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%2Fp2mjjvn0m2abc6sk4jxd.jpg" alt=" "&gt;&lt;/a&gt;&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%2Fy8mlbjdmwq3bdg159iqq.jpg" 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%2Fy8mlbjdmwq3bdg159iqq.jpg" alt=" "&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  🔹 Important Terms
&lt;/h2&gt;

&lt;h2&gt;
  
  
  🔸 HEAD
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Pointer to the &lt;strong&gt;first node&lt;/strong&gt; of the list
&lt;/li&gt;
&lt;li&gt;Entry point to access the linked list
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔸 NODE
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Basic unit of a linked list
&lt;/li&gt;
&lt;li&gt;Contains:

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Data&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pointer (next address)&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔸 POINTER / REFERENCE
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Stores the &lt;strong&gt;address of another node&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Used to connect nodes together
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔸 NULL
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Indicates &lt;strong&gt;end of the list&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Means no next node exists
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔸 TRAVERSAL
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Process of &lt;strong&gt;visiting each node&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Starts from &lt;strong&gt;head&lt;/strong&gt; and moves till &lt;code&gt;NULL&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔸 LENGTH
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Total &lt;strong&gt;number of nodes&lt;/strong&gt; in the list
&lt;/li&gt;
&lt;li&gt;Found by traversing the entire list
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔸 NEXT
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Pointer inside a node
&lt;/li&gt;
&lt;li&gt;Stores address of the &lt;strong&gt;next node&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔸 DYNAMIC MEMORY
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Memory is &lt;strong&gt;allocated as needed&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;No need for fixed size
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔸 NON-CONTIGUOUS MEMORY
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Nodes are stored in &lt;strong&gt;random memory locations&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Not stored in sequence like arrays
&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%2Fs9mmqbasknp8obxwgkj4.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%2Fs9mmqbasknp8obxwgkj4.png" alt=" "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  📌 What is a Node?
&lt;/h1&gt;

&lt;p&gt;👉 A node is like a Container which contains data and a reference to the next node in a linked list.&lt;/p&gt;

&lt;p&gt;Think of a node as a &lt;strong&gt;basic building block&lt;/strong&gt; used to create a &lt;strong&gt;linked list&lt;/strong&gt;.&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%2Fdubwqosvpquafzdrku1v.webp" 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%2Fdubwqosvpquafzdrku1v.webp" alt=" "&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  🔹 Simple Real-Life Meaning
&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%2F3hgpp7fcrycvmbmiv32f.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%2F3hgpp7fcrycvmbmiv32f.png" alt=" "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Imagine a &lt;strong&gt;train&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each &lt;strong&gt;coach (bogie)&lt;/strong&gt; = a node
&lt;/li&gt;
&lt;li&gt;Inside each coach = &lt;strong&gt;data (passengers)&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Connection between coaches = &lt;strong&gt;pointer (link)&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, the train works only because all coaches are connected.&lt;/p&gt;

&lt;p&gt;👉 Similarly, a linked list works because all nodes are connected.&lt;/p&gt;

&lt;h2&gt;
  
  
  🔹 What Does a Node Contain?
&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%2Fxtmf63zw1m8krbym1ahz.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%2Fxtmf63zw1m8krbym1ahz.png" alt=" "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A node has &lt;strong&gt;two parts&lt;/strong&gt;:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. 📦 Data (Value)
&lt;/h3&gt;

&lt;p&gt;This is the actual information stored in the node.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;10
&lt;/li&gt;
&lt;li&gt;50
&lt;/li&gt;
&lt;li&gt;"A"
&lt;/li&gt;
&lt;li&gt;100
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  2. 🔗 Link (Pointer)
&lt;/h3&gt;

&lt;p&gt;This stores the &lt;strong&gt;address of the next node&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It tells:&lt;br&gt;
👉 “Where is the next node?”&lt;/p&gt;


&lt;h2&gt;
  
  
  🔹 Structure of a Node
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[ Data | Next ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Data&lt;/strong&gt; → value stored in the node&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Next&lt;/strong&gt; → address of the next node&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  🔹 Example of Linked Nodes
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10 → 20 → 30 → 40 → NULL
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2F4jmlhm0dq7ejmwmprp4d.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%2F4jmlhm0dq7ejmwmprp4d.png" alt=" "&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Explanation:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Each value is stored in a separate node&lt;/li&gt;
&lt;li&gt;Each node is connected using a pointer&lt;/li&gt;
&lt;li&gt;The last node points to &lt;strong&gt;NULL (end of list)&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;__&lt;/p&gt;
&lt;h2&gt;
  
  
  🎯 3. Core Idea (Very Important)
&lt;/h2&gt;

&lt;p&gt;👉 To create a node in a singly linked list, we &lt;code&gt;create&lt;/code&gt; an &lt;code&gt;object&lt;/code&gt; &lt;code&gt;that contains&lt;/code&gt; two properties: &lt;code&gt;value&lt;/code&gt; and &lt;code&gt;next&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; Instead of creating objects manually, we use a constructor function or class. Using the new keyword, JavaScript creates a new object, assigns value and next, and returns the node.&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%2F9ilb6fqrpjmhuz100cxq.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%2F9ilb6fqrpjmhuz100cxq.png" alt=" "&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;👉 This object has:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;data (&lt;code&gt;value&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;pointer (&lt;code&gt;next&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ❌ 4. Problem with Manual Creation
&lt;/h2&gt;

&lt;p&gt;If we create nodes manually:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;node1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;node2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;👉 Problems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Repetitive ❌&lt;/li&gt;
&lt;li&gt;Time-consuming ❌&lt;/li&gt;
&lt;li&gt;Not scalable ❌&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ✅ 5. Solution → Use Function / Class
&lt;/h2&gt;

&lt;p&gt;👉 Instead of creating objects manually, we use a &lt;strong&gt;constructor function&lt;/strong&gt; (or class)&lt;/p&gt;

&lt;p&gt;So:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We &lt;strong&gt;don’t create objects ourselves&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;We just &lt;strong&gt;call a function&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;It creates the object for us automatically ✅&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🏗️ 6. Final Code (Constructor Function)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// store data&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// store reference to next node&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🧠 7. What is Happening Internally?
&lt;/h2&gt;

&lt;p&gt;When you write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;node1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;👉 JavaScript performs these steps:&lt;/p&gt;

&lt;h3&gt;
  
  
  🔥 Step-by-step (Dry Run)
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Create empty object:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;this&lt;/code&gt; points to that object:
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;```javascript id="step2"&lt;br&gt;
this = {}&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;


3. Assign values:



```javascript id="step3"
this.value = 5
this.next = null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Return object:
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;```javascript id="step4"&lt;br&gt;
{&lt;br&gt;
  value: 5,&lt;br&gt;
  next: null&lt;br&gt;
}&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;


👉 So finally:



```javascript id="final-object"
node1 = {
  value: 5,
  next: null
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🔁 8. Creating Multiple Nodes (Key Idea)
&lt;/h2&gt;

&lt;p&gt;👉 You don’t create objects manually again and again&lt;/p&gt;

&lt;p&gt;Instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;node1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;node2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;node3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;👉 Every time you call:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;➡️ A &lt;strong&gt;new object is created automatically&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  🔗 9. Connecting Nodes
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;node1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;node2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;node2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;node3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🖼️ 10. Visualization
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[5 | next] → [10 | next] → [20 | null]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🧠 11. What is &lt;code&gt;this&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;👉 &lt;code&gt;this&lt;/code&gt; = current object being created&lt;/p&gt;

&lt;p&gt;It allows us to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Put data inside object&lt;/li&gt;
&lt;li&gt;Build structure dynamically&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🚀 12. Why &lt;code&gt;new&lt;/code&gt; Keyword is MUST
&lt;/h2&gt;

&lt;p&gt;👉 &lt;code&gt;new&lt;/code&gt; does everything automatically:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Creates object&lt;/li&gt;
&lt;li&gt;Links &lt;code&gt;this&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Executes function&lt;/li&gt;
&lt;li&gt;Returns object&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  ❌ Without &lt;code&gt;new&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;node1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;👉 Wrong because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No object creation ❌&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;this&lt;/code&gt; breaks ❌&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🧠 13. Memory Concept (Interview Level)
&lt;/h2&gt;

&lt;p&gt;👉 &lt;code&gt;next&lt;/code&gt; does NOT store full node&lt;br&gt;
👉 It stores &lt;strong&gt;reference (address)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node1 → { value: 5, next: address of node2 }
node2 → { value: 10, next: null }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  ⚠️ 14. Common Mistake
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// ❌ wrong&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;👉 Always:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🧪 15. Class Version (Modern JavaScript)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&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;p&gt;👉 Same working, cleaner syntax ✅&lt;/p&gt;

&lt;h2&gt;
  
  
  🔹 Easy Way to Understand
&lt;/h2&gt;

&lt;p&gt;Think of a &lt;strong&gt;treasure hunt game&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Each clue contains:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;🏆 Reward (data)&lt;/li&gt;
&lt;li&gt;📍 Next location (pointer)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;You keep moving from one clue to the next.&lt;/p&gt;

&lt;p&gt;👉 This is exactly how nodes work.&lt;/p&gt;

&lt;h1&gt;
  
  
  🔹 Why Linked List is Important?
&lt;/h1&gt;

&lt;p&gt;A linked list is important because it provides a &lt;strong&gt;dynamic and flexible way of storing data&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It is used when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Size of data is unknown in advance&lt;/li&gt;
&lt;li&gt;Frequent insertion and deletion is required&lt;/li&gt;
&lt;li&gt;Memory should be used efficiently at runtime&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔹 Advantages of Linked List
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;📌 Dynamic size (can grow and shrink at runtime)&lt;/li&gt;
&lt;li&gt;📌 No need for continuous (contiguous) memory&lt;/li&gt;
&lt;li&gt;📌 Easy insertion and deletion (no shifting required)&lt;/li&gt;
&lt;li&gt;📌 Memory is allocated only when needed&lt;/li&gt;
&lt;li&gt;📌 Useful for implementing Stack, Queue, Graph, and Hash collision handling&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  🔥 Linked List vs Array (Very Important for Exams &amp;amp; Interviews)
&lt;/h1&gt;




&lt;h2&gt;
  
  
  🔹 1. Basic Structure Difference
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Array
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Stores elements in &lt;strong&gt;continuous (contiguous) memory&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Uses index-based access
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Index:  0   1   2   3
Array:  3   4   5   1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Linked List
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Stores elements in &lt;strong&gt;non-contiguous memory&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Uses nodes (Data + Pointer)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3 → 4 → 5 → 1 → NULL
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🔹 2. Node Structure (Core Concept)
&lt;/h2&gt;

&lt;p&gt;A node contains:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[ Data | Pointer ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Data → actual value&lt;/li&gt;
&lt;li&gt;Pointer → address of next node&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 In doubly linked list:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[ Prev | Data | Next ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🔹 3. Memory Difference
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Array (Contiguous Memory)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;| 3 | 4 | 5 | 1 |
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Memory is continuous&lt;/li&gt;
&lt;li&gt;No extra pointer memory&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Linked List (Non-Contiguous Memory)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
[3 | • ] → [4 | • ] → [5 | • ] → [1 | NULL]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Nodes are stored randomly in memory&lt;/li&gt;
&lt;li&gt;Extra memory used for pointers&lt;/li&gt;
&lt;li&gt;Doubly linked list uses more memory than singly linked list&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔹 4. Example (3, 4, 5, 1)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Array
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Index:  0   1   2   3
Value:  3   4   5   1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Linked List
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3 → 4 → 5 → 1 → NULL
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🔹 5. Accessing Elements
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Array → Fast Access
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr[2] = 5 → O(1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✔ Direct index access&lt;br&gt;
✔ Very fast&lt;/p&gt;


&lt;h3&gt;
  
  
  Linked List → Slow Access
&lt;/h3&gt;

&lt;p&gt;To access an element:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3 → 4 → 5 → 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To reach "5":&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start from head&lt;/li&gt;
&lt;li&gt;Traverse node by node&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;❌ No direct access&lt;br&gt;
✔ Time Complexity: O(n)&lt;/p&gt;


&lt;h2&gt;
  
  
  🔹 6. Insertion &amp;amp; Deletion
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Linked List → Easy
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;No shifting required&lt;/li&gt;
&lt;li&gt;Only pointer changes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3 → 4 → 5 → 1
Insert 10 between 4 and 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✔ Efficient (O(1) if position known)&lt;/p&gt;




&lt;h3&gt;
  
  
  Array → Difficult
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Requires shifting elements
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3 4 5 1
Insert 10 at index 1 → shift elements
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;❌ Time Complexity: O(n)&lt;/p&gt;




&lt;h2&gt;
  
  
  🔹 7. Memory Usage
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Structure&lt;/th&gt;
&lt;th&gt;Memory&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Array&lt;/td&gt;
&lt;td&gt;Efficient (no pointers)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Singly Linked List&lt;/td&gt;
&lt;td&gt;Extra memory (1 pointer/node)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Doubly Linked List&lt;/td&gt;
&lt;td&gt;More memory (2 pointers/node)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  🔹 8. Time Complexity Comparison
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operation&lt;/th&gt;
&lt;th&gt;Array&lt;/th&gt;
&lt;th&gt;Linked List&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Access&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Search&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Insert (beginning)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Insert (middle)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(1)*&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Delete&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(1)*&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;if node reference is known&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔹 9. Key Differences (Quick Revision)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Array:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Fast access&lt;/li&gt;
&lt;li&gt;Fixed size (or costly resizing)&lt;/li&gt;
&lt;li&gt;Contiguous memory&lt;/li&gt;
&lt;li&gt;Poor insertion/deletion&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Linked List:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Slow access&lt;/li&gt;
&lt;li&gt;Dynamic size&lt;/li&gt;
&lt;li&gt;Non-contiguous memory&lt;/li&gt;
&lt;li&gt;Easy insertion/deletion&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔹 10. Real-Life Use Cases
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Array:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Image processing&lt;/li&gt;
&lt;li&gt;Index-based systems&lt;/li&gt;
&lt;li&gt;Static data storage&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Linked List:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Music playlist&lt;/li&gt;
&lt;li&gt;Browser history&lt;/li&gt;
&lt;li&gt;Undo/Redo system&lt;/li&gt;
&lt;li&gt;Memory management&lt;/li&gt;
&lt;li&gt;Graph adjacency list&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  🟡 HOW LINKED LIST STORES DATA IN MEMORY
&lt;/h1&gt;

&lt;h3&gt;
  
  
  🔹 Basic Idea
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Nodes are stored in &lt;strong&gt;random (non-contiguous) memory locations&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Each node contains:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Data&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pointer (address) to the next node&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Connection is &lt;strong&gt;logical (via pointers), not physical&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;




&lt;h3&gt;
  
  
  📌 Structure of a Node
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[ Data | Next Address ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  📌 Memory Representation
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Memory Addresses:   100    500    200    800

Nodes:
[10 | 500] → [20 | 200] → [30 | 800] → [40 | NULL]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;100&lt;/code&gt; stores data &lt;code&gt;10&lt;/code&gt; and points to &lt;code&gt;500&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;500&lt;/code&gt; stores data &lt;code&gt;20&lt;/code&gt; and points to &lt;code&gt;200&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;200&lt;/code&gt; stores data &lt;code&gt;30&lt;/code&gt; and points to &lt;code&gt;800&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;800&lt;/code&gt; stores data &lt;code&gt;40&lt;/code&gt; and points to &lt;code&gt;NULL&lt;/code&gt; (end of list)&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  🔹 Important Points
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;first node is accessed using a pointer called &lt;code&gt;head&lt;/code&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;last node points to &lt;code&gt;NULL&lt;/code&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Nodes are &lt;strong&gt;not stored sequentially in memory&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Order is maintained using &lt;strong&gt;addresses (links)&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  🔹 Advantages
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Dynamic size&lt;/strong&gt; (can grow/shrink easily)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Efficient insertion/deletion&lt;/strong&gt; (no shifting required)&lt;/li&gt;
&lt;li&gt;Memory is used &lt;strong&gt;as needed&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  🔹 Disadvantages
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Extra memory&lt;/strong&gt; needed for pointers&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No direct access&lt;/strong&gt; (must traverse from head)&lt;/li&gt;
&lt;li&gt;Slower access compared to arrays&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  ⚡ Key Comparison with Array
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Array&lt;/th&gt;
&lt;th&gt;Linked List&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Memory&lt;/td&gt;
&lt;td&gt;Contiguous&lt;/td&gt;
&lt;td&gt;Non-contiguous&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Size&lt;/td&gt;
&lt;td&gt;Fixed&lt;/td&gt;
&lt;td&gt;Dynamic&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Access&lt;/td&gt;
&lt;td&gt;Direct (fast)&lt;/td&gt;
&lt;td&gt;Sequential (slow)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Insertion&lt;/td&gt;
&lt;td&gt;Costly (shift)&lt;/td&gt;
&lt;td&gt;Easy (pointer change)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h1&gt;
  
  
  🟡 TYPES OF LINKED LIST (DETAILED)
&lt;/h1&gt;

&lt;p&gt;Linked List is a linear data structure where elements (nodes) are connected using pointers.&lt;/p&gt;

&lt;h1&gt;
  
  
  🔹 1. SINGLY LINKED LIST (SLL)
&lt;/h1&gt;

&lt;h2&gt;
  
  
  📌 Definition
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Each node contains:

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Data&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;One pointer (Next)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Points only to the &lt;strong&gt;next node&lt;/strong&gt;
&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  📌 Structure
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
[Data | Next] → [Data | Next] → [Data | NULL]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  📌 Key Points
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Traversal is &lt;strong&gt;only forward&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Last node points to &lt;strong&gt;NULL&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Simplest type of linked list&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  ✅ Advantages
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Less memory (only one pointer)&lt;/li&gt;
&lt;li&gt;Easy to implement&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  ❌ Disadvantages
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Cannot move backward&lt;/li&gt;
&lt;li&gt;Traversal is slow (O(n))&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  💡 Use Cases
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Stacks&lt;/li&gt;
&lt;li&gt;Basic dynamic memory structures&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  🔹 2. DOUBLY LINKED LIST (DLL)
&lt;/h1&gt;

&lt;h2&gt;
  
  
  📌 Definition
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Each node contains:

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Previous pointer&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Next pointer&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  📌 Structure
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
NULL ← [Prev | Data | Next] ⇄ [Prev | Data | Next] → NULL

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  📌 Key Points
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Traversal in &lt;strong&gt;both directions&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Extra memory needed for &lt;code&gt;prev&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  ✅ Advantages
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Easy backward traversal&lt;/li&gt;
&lt;li&gt;Easier deletion (no need to track previous node)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  ❌ Disadvantages
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;More memory usage&lt;/li&gt;
&lt;li&gt;More complex&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  💡 Use Cases
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Navigation systems (back/forward)&lt;/li&gt;
&lt;li&gt;Undo/Redo operations&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  🔹 3. CIRCULAR LINKED LIST (CLL)
&lt;/h1&gt;

&lt;h2&gt;
  
  
  📌 Definition
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Last node connects back to &lt;strong&gt;first node&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;No node points to NULL&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  📌 Structure
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
[Data | Next] → [Data | Next] → [Data | Next]
↑__________________________________↓

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  📌 Key Points
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Forms a &lt;strong&gt;loop&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Can start traversal from &lt;strong&gt;any node&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  ✅ Advantages
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;No NULL pointer issues&lt;/li&gt;
&lt;li&gt;Efficient for cyclic processes&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  ❌ Disadvantages
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Infinite loop risk&lt;/li&gt;
&lt;li&gt;Harder to debug&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  💡 Use Cases
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Round-robin scheduling&lt;/li&gt;
&lt;li&gt;Multiplayer games (turn rotation)&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  🔹 4. CIRCULAR DOUBLY LINKED LIST (CDLL)
&lt;/h1&gt;

&lt;h2&gt;
  
  
  📌 Definition
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Combination of &lt;strong&gt;DLL + Circular&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Each node has:

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Prev pointer&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Next pointer&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;First and last nodes are connected&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  📌 Structure
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
↔ [Prev | Data | Next] ⇄ [Prev | Data | Next] ↔
↑____________________________________________↓

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  📌 Key Points
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Traversal in &lt;strong&gt;both directions&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;No NULL pointer&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  ✅ Advantages
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Full flexibility (forward + backward)&lt;/li&gt;
&lt;li&gt;Efficient for continuous navigation&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  ❌ Disadvantages
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Most complex&lt;/li&gt;
&lt;li&gt;Highest memory usage&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  💡 Use Cases
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Advanced navigation systems&lt;/li&gt;
&lt;li&gt;Music/playlist looping&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  🧠 IMPORTANT INTERVIEW POINTS
&lt;/h1&gt;

&lt;h2&gt;
  
  
  🔸 Difference Between Types
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Singly LL&lt;/th&gt;
&lt;th&gt;Doubly LL&lt;/th&gt;
&lt;th&gt;Circular LL&lt;/th&gt;
&lt;th&gt;Circular Doubly LL&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Pointers&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Direction&lt;/td&gt;
&lt;td&gt;One-way&lt;/td&gt;
&lt;td&gt;Two-way&lt;/td&gt;
&lt;td&gt;One-way&lt;/td&gt;
&lt;td&gt;Two-way&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;NULL Present&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Memory Usage&lt;/td&gt;
&lt;td&gt;Low&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;td&gt;Low&lt;/td&gt;
&lt;td&gt;High&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Complexity&lt;/td&gt;
&lt;td&gt;Easy&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;td&gt;Hard&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  🔸 Time Complexity (Common)
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operation&lt;/th&gt;
&lt;th&gt;SLL&lt;/th&gt;
&lt;th&gt;DLL&lt;/th&gt;
&lt;th&gt;CLL&lt;/th&gt;
&lt;th&gt;CDLL&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Traversal&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Insertion (head)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Deletion&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(1)*&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(1)*&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;*O(1) deletion in DLL/CDLL if node is known&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  🔸 When to Use Which?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;Singly LL&lt;/strong&gt; → when memory is limited
&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;Doubly LL&lt;/strong&gt; → when backward traversal needed
&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;Circular LL&lt;/strong&gt; → when process is cyclic
&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;Circular Doubly LL&lt;/strong&gt; → when full flexibility needed
&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  🟣 7. LINKED LIST VS ARRAY (DEEP)
&lt;/h1&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Array&lt;/th&gt;
&lt;th&gt;Linked List&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Memory&lt;/td&gt;
&lt;td&gt;Contiguous&lt;/td&gt;
&lt;td&gt;Non-contiguous&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Size&lt;/td&gt;
&lt;td&gt;Fixed / Resizable&lt;/td&gt;
&lt;td&gt;Fully dynamic&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Access&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Insert/Delete&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(1) (if pointer known)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cache Friendly&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Memory Overhead&lt;/td&gt;
&lt;td&gt;Low&lt;/td&gt;
&lt;td&gt;High (pointers)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h1&gt;
  
  
  🟣 8. TIME COMPLEXITY CHEAT SHEET
&lt;/h1&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operation&lt;/th&gt;
&lt;th&gt;Time&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Access&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Search&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Insert at head&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Insert at tail&lt;/td&gt;
&lt;td&gt;O(1)*&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Delete&lt;/td&gt;
&lt;td&gt;O(1)*&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;if pointer available&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  🟠 9. CORE OPERATIONS (MUST MASTER)
&lt;/h1&gt;




&lt;h2&gt;
  
  
  📌 Traversal
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Visit each node one by one&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  📌 Insertion
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;At beginning&lt;/li&gt;
&lt;li&gt;At end&lt;/li&gt;
&lt;li&gt;At position&lt;/li&gt;
&lt;li&gt;After node&lt;/li&gt;
&lt;li&gt;Before node&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  📌 Deletion
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;From beginning&lt;/li&gt;
&lt;li&gt;From end&lt;/li&gt;
&lt;li&gt;By value&lt;/li&gt;
&lt;li&gt;By position&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  📌 Update
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Change node value&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  📌 Search
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Linear search&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  🟠 10. EDGE CASES (VERY IMPORTANT)
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Empty list&lt;/li&gt;
&lt;li&gt;Single node list&lt;/li&gt;
&lt;li&gt;Two nodes&lt;/li&gt;
&lt;li&gt;Deleting head&lt;/li&gt;
&lt;li&gt;Deleting tail&lt;/li&gt;
&lt;li&gt;Loop present&lt;/li&gt;
&lt;li&gt;Duplicate values&lt;/li&gt;
&lt;li&gt;Large inputs&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ⚡ When to Use &lt;strong&gt;Array&lt;/strong&gt;
&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%2F2npeqqhmmp2uv0wkx20q.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%2F2npeqqhmmp2uv0wkx20q.png" alt=" "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  ✅ Use Array When:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Fast Access is Required (O(1))&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;You need to directly jump to any element
&lt;/li&gt;
&lt;li&gt;Example: &lt;code&gt;arr[4]&lt;/code&gt; instantly gives value
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;🏢 Analogy:&lt;br&gt;&lt;br&gt;
   You know the exact flat number → go directly there&lt;/p&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Data Size is Fixed or Known&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Size doesn’t change frequently
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Marks of 100 students
&lt;/li&gt;
&lt;li&gt;Days in a week
&lt;/li&gt;
&lt;/ul&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Frequent Read Operations&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;More reading, less inserting/deleting
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Better Cache Performance&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Stored in continuous memory → faster in real systems
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;


&lt;h3&gt;
  
  
  ❌ Avoid Array When:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Frequent insertions/deletions in middle
&lt;/li&gt;
&lt;li&gt;Size changes dynamically
&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  🔗 When to Use &lt;strong&gt;Linked List&lt;/strong&gt;
&lt;/h2&gt;
&lt;h3&gt;
  
  
  ✅ Use Linked List When:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Frequent Insertions/Deletions&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;No shifting needed
&lt;/li&gt;
&lt;li&gt;Just update pointers
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;🏠 Analogy:&lt;br&gt;&lt;br&gt;
   Insert a new house → just change address slips  &lt;/p&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Dynamic Size&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Can grow/shrink anytime
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Memory is Fragmented&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Doesn’t require continuous space
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Implementing Advanced Structures&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Stacks
&lt;/li&gt;
&lt;li&gt;Queues
&lt;/li&gt;
&lt;li&gt;Graphs
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;


&lt;h3&gt;
  
  
  ❌ Avoid Linked List When:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You need fast random access
&lt;/li&gt;
&lt;li&gt;Extra memory (pointer storage) is a concern
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;//// sdfsegfs&lt;/p&gt;
&lt;h1&gt;
  
  
  🔴 11. IMPORTANT PATTERNS
&lt;/h1&gt;


&lt;h2&gt;
  
  
  🧠 1. Two Pointer Technique
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Slow &amp;amp; Fast pointer&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Used in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cycle detection&lt;/li&gt;
&lt;li&gt;Middle node&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  🧠 2. Reversal Pattern
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Iterative&lt;/li&gt;
&lt;li&gt;Recursive&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  🧠 3. Dummy Node Technique
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Simplifies insertion/deletion&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  🧠 4. Sliding Pointer
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Used in partition problems&lt;/li&gt;
&lt;/ul&gt;


&lt;h1&gt;
  
  
  🔴 12. MUST-DO PROBLEMS (INTERVIEW CORE)
&lt;/h1&gt;


&lt;h2&gt;
  
  
  Easy
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Reverse Linked List&lt;/li&gt;
&lt;li&gt;Find middle&lt;/li&gt;
&lt;li&gt;Remove duplicates&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  Medium
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Detect cycle (Floyd)&lt;/li&gt;
&lt;li&gt;Merge two sorted lists&lt;/li&gt;
&lt;li&gt;Remove nth from end&lt;/li&gt;
&lt;li&gt;Intersection of two lists&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  Hard
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;LRU Cache 🔥&lt;/li&gt;
&lt;li&gt;Clone list with random pointer&lt;/li&gt;
&lt;li&gt;Reverse in k-groups&lt;/li&gt;
&lt;li&gt;Flatten linked list&lt;/li&gt;
&lt;/ul&gt;


&lt;h1&gt;
  
  
  🔥 13. ADVANCED CONCEPTS
&lt;/h1&gt;


&lt;h2&gt;
  
  
  📌 Floyd Cycle Detection
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Fast moves 2 steps&lt;/li&gt;
&lt;li&gt;Slow moves 1 step&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  📌 Random Pointer Linked List
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Each node points randomly&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  📌 Skip List
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Multi-level linked list&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  📌 XOR Linked List (Rare)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Memory optimized&lt;/li&gt;
&lt;/ul&gt;


&lt;h1&gt;
  
  
  🔥 14. DOUBLY LINKED LIST (DETAIL)
&lt;/h1&gt;
&lt;h3&gt;
  
  
  Node:
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Prev | Data | Next]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Pros:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Backward traversal&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Cons:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Extra memory&lt;/li&gt;
&lt;/ul&gt;


&lt;h1&gt;
  
  
  🔥 15. CIRCULAR LINKED LIST (DETAIL)
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;No NULL&lt;/li&gt;
&lt;li&gt;Last connects to first&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Used in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Round-robin scheduling&lt;/li&gt;
&lt;/ul&gt;


&lt;h1&gt;
  
  
  🧠 16. MEMORY UNDERSTANDING
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Each node allocated dynamically&lt;/li&gt;
&lt;li&gt;Uses heap memory&lt;/li&gt;
&lt;li&gt;Pointer stores address&lt;/li&gt;
&lt;/ul&gt;


&lt;h1&gt;
  
  
  🧠 17. COMMON MISTAKES
&lt;/h1&gt;

&lt;p&gt;❌ Forgetting NULL check&lt;br&gt;
❌ Losing head pointer&lt;br&gt;
❌ Infinite loop in circular list&lt;br&gt;
❌ Wrong pointer update order&lt;/p&gt;


&lt;h1&gt;
  
  
  ⚔️ 18. INTERVIEW STRATEGY
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;Draw diagram first ✍️&lt;/li&gt;
&lt;li&gt;Handle edge cases&lt;/li&gt;
&lt;li&gt;Use dummy node&lt;/li&gt;
&lt;li&gt;Dry run example&lt;/li&gt;
&lt;/ol&gt;


&lt;h1&gt;
  
  
  🚀 19. REAL WORLD USE CASES
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Music playlist&lt;/li&gt;
&lt;li&gt;Browser history&lt;/li&gt;
&lt;li&gt;Undo/Redo&lt;/li&gt;
&lt;li&gt;Navigation systems&lt;/li&gt;
&lt;/ul&gt;


&lt;h1&gt;
  
  
  💰 ₹5 CRORE ANALOGY (Linked List Explained)
&lt;/h1&gt;
&lt;h2&gt;
  
  
  🏠 Concept Mapping
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;House&lt;/strong&gt; = Node
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Money (₹)&lt;/strong&gt; = Data
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Address Slip&lt;/strong&gt; = Pointer (&lt;code&gt;next&lt;/code&gt;)
&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  🔗 Structure
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
House1 → House2 → House3 → House4 → House5 → NULL

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🧱 Inside Each House (Node)
&lt;/h2&gt;

&lt;p&gt;Each house contains:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;💰 Money (Data)&lt;/li&gt;
&lt;li&gt;📄 Address slip pointing to the next house (Pointer)&lt;/li&gt;
&lt;/ol&gt;


&lt;h3&gt;
  
  
  🏠 House Breakdown
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;House1&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Money: ₹1 crore
&lt;/li&gt;
&lt;li&gt;Next: House2
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;House2&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Money: ₹1 crore
&lt;/li&gt;
&lt;li&gt;Next: House3
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;House3&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Money: ₹1 crore
&lt;/li&gt;
&lt;li&gt;Next: House4
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;House4&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Money: ₹1 crore
&lt;/li&gt;
&lt;li&gt;Next: House5
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;House5&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Money: ₹1 crore
&lt;/li&gt;
&lt;li&gt;Next: NULL (end of list)
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  🧠 Key Idea: Sequential Access
&lt;/h2&gt;

&lt;p&gt;You &lt;strong&gt;cannot jump directly&lt;/strong&gt; to a house.&lt;/p&gt;

&lt;p&gt;To reach House5:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
House1 → House2 → House3 → House4 → House5

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;➡️ You must follow the chain step-by-step.&lt;/p&gt;




&lt;h2&gt;
  
  
  🧾 Programming Mapping
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Real World&lt;/th&gt;
&lt;th&gt;Programming&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;House&lt;/td&gt;
&lt;td&gt;Node&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Money&lt;/td&gt;
&lt;td&gt;Data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Address Slip&lt;/td&gt;
&lt;td&gt;Pointer (&lt;code&gt;next&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;First House&lt;/td&gt;
&lt;td&gt;Head&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Last House&lt;/td&gt;
&lt;td&gt;Tail (&lt;code&gt;next = NULL&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  💡 Advantages
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Dynamic Size
&lt;/h3&gt;

&lt;p&gt;You can easily add more houses:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
House5 → House6 → NULL

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Easy Insertion
&lt;/h3&gt;

&lt;p&gt;Insert a new house between House2 and House3:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
House2 → NewHouse → House3

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;➡️ Only pointers change, no shifting needed.&lt;/p&gt;




&lt;h2&gt;
  
  
  ⚠️ Limitation
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Slow access
&lt;/li&gt;
&lt;li&gt;To reach House5, you must pass through all previous houses
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🎯 Final Intuition
&lt;/h2&gt;

&lt;p&gt;Think of it as a &lt;strong&gt;treasure trail&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each house gives you money 💰
&lt;/li&gt;
&lt;li&gt;And tells you where to go next 📍
&lt;/li&gt;
&lt;li&gt;If you lose the address slip, the chain breaks ❌
&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Backend Testing — Complete Notes</title>
      <dc:creator>Abhishek Gupta </dc:creator>
      <pubDate>Fri, 03 Apr 2026 19:57:52 +0000</pubDate>
      <link>https://forem.com/abhishek-nexgen-dev/backend-testing-complete-notes-4179</link>
      <guid>https://forem.com/abhishek-nexgen-dev/backend-testing-complete-notes-4179</guid>
      <description>&lt;h1&gt;
  
  
  📌 1. What is Backend Testing?
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Definition:
&lt;/h3&gt;

&lt;p&gt;Backend testing means &lt;strong&gt;checking whether your server-side logic works correctly in all situations&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Simple Understanding:
&lt;/h3&gt;

&lt;p&gt;When a user sends request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User → API → Backend → Database → Response
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;👉 You must ensure:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Correct response is returned&lt;/li&gt;
&lt;li&gt;Errors are handled properly&lt;/li&gt;
&lt;li&gt;Data is stored correctly&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  ❗ 2. Why Backend Testing is Important
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Without Testing:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Bugs go to production ❌&lt;/li&gt;
&lt;li&gt;Users face crashes ❌&lt;/li&gt;
&lt;li&gt;Data corruption ❌&lt;/li&gt;
&lt;li&gt;Security issues ❌&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  With Testing:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Bugs caught early ✅&lt;/li&gt;
&lt;li&gt;Safe deployment ✅&lt;/li&gt;
&lt;li&gt;Confident coding ✅&lt;/li&gt;
&lt;li&gt;Faster development ✅&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Real Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Case: User signup API
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without testing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Duplicate emails allowed ❌&lt;/li&gt;
&lt;li&gt;Password not validated ❌&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With testing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Duplicate → rejected ✅&lt;/li&gt;
&lt;li&gt;Invalid input → error ✅&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  🧠 3. What Should You Test?
&lt;/h1&gt;

&lt;p&gt;You should test:&lt;/p&gt;

&lt;h2&gt;
  
  
  ✅ Functional Behavior
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;API works correctly&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  ✅ Validation
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Wrong input handled&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  ✅ Security
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Unauthorized access blocked&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  ✅ Database
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Data saved correctly&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  ✅ Errors
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Server errors handled&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  🧩 4. Types of Backend Testing
&lt;/h1&gt;




&lt;h2&gt;
  
  
  🔹 1. Unit Testing
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Definition:
&lt;/h3&gt;

&lt;p&gt;Test &lt;strong&gt;single function in isolation&lt;/strong&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&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;p&gt;Test:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Features:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Fast ⚡&lt;/li&gt;
&lt;li&gt;No DB&lt;/li&gt;
&lt;li&gt;No API&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔹 2. Integration Testing (MOST IMPORTANT)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Definition:
&lt;/h3&gt;

&lt;p&gt;Test multiple layers together&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Route → Controller → Service → DB
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;POST&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Test:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Data sent&lt;/li&gt;
&lt;li&gt;Saved in DB&lt;/li&gt;
&lt;li&gt;Response returned&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔹 3. End-to-End Testing (E2E)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Definition:
&lt;/h3&gt;

&lt;p&gt;Test full user journey&lt;/p&gt;




&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Register → Login → Access profile
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Features:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Real-world testing&lt;/li&gt;
&lt;li&gt;Slower&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  🏗️ 5. Backend Testing Tools
&lt;/h1&gt;

&lt;h2&gt;
  
  
  🥇 Core Tools
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Jest&lt;/li&gt;
&lt;li&gt;Supertest&lt;/li&gt;
&lt;li&gt;MongoDB Memory Server&lt;/li&gt;
&lt;li&gt;dotenv&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  🧠 6. Role of Each Tool
&lt;/h1&gt;

&lt;h2&gt;
  
  
  🧪 Jest
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Runs tests&lt;/li&gt;
&lt;li&gt;Provides &lt;code&gt;describe&lt;/code&gt;, &lt;code&gt;it&lt;/code&gt;, &lt;code&gt;expect&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Handles assertions&lt;/li&gt;
&lt;li&gt;Supports mocking&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🌐 Supertest
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Sends HTTP requests&lt;/li&gt;
&lt;li&gt;Tests APIs like Postman&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🗄️ MongoMemoryServer
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Fake DB&lt;/li&gt;
&lt;li&gt;Fast &amp;amp; isolated&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ⚙️ dotenv
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Separate test environment&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  ⚙️ 7. How Testing Works (Flow)
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Jest starts test
 ↓
Supertest sends request
 ↓
Backend processes request
 ↓
DB stores data
 ↓
Response returned
 ↓
Jest verifies result
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  🧱 8. Basic Setup
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Install:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save-dev&lt;/span&gt; jest supertest mongodb-memory-server dotenv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  package.json
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"jest"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  🧪 9. First Test Example
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;request&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;supertest&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../app&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;GET /&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;should return message&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;request&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;API working&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&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;h1&gt;
  
  
  🔥 10. Real Backend Test Cases
&lt;/h1&gt;

&lt;h2&gt;
  
  
  ✅ Success Case
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  ❌ Validation Error
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🔒 Unauthorized
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;401&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🔁 Duplicate Data
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toMatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/already exists/i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  💥 Server Error
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  🔐 11. Authentication Testing
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;beforeAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;request&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/login&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;password&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nx"&gt;token&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;should access protected route&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;request&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/profile&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Authorization&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;`Bearer &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&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;h1&gt;
  
  
  🧠 12. Database Testing
&lt;/h1&gt;

&lt;p&gt;Use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;MongoDB Memory Server&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Why:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;No real DB&lt;/li&gt;
&lt;li&gt;Fast&lt;/li&gt;
&lt;li&gt;Clean&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  🧪 13. Mocking
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Why needed?
&lt;/h3&gt;

&lt;p&gt;To avoid calling real services&lt;/p&gt;




&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../emailService&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;sendEmail&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fn&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;h1&gt;
  
  
  ⚡ 14. Best Practices
&lt;/h1&gt;

&lt;h2&gt;
  
  
  ✅ Do:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Test real API behavior&lt;/li&gt;
&lt;li&gt;Cover edge cases&lt;/li&gt;
&lt;li&gt;Keep tests independent&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ❌ Don’t:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use real DB&lt;/li&gt;
&lt;li&gt;Start server&lt;/li&gt;
&lt;li&gt;Ignore errors&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  🧠 15. Testing Pyramid
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Unit → Most
Integration → Medium
E2E → Few
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  📊 16. Code Coverage
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;test&lt;/span&gt; &lt;span class="nt"&gt;--&lt;/span&gt; &lt;span class="nt"&gt;--coverage&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  ⚙️ 17. CI/CD Testing
&lt;/h1&gt;

&lt;p&gt;Use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;GitHub Actions&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  🧠 18. Interview Questions
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Q: Why Jest?
&lt;/h2&gt;

&lt;p&gt;👉 Easy + powerful&lt;/p&gt;




&lt;h2&gt;
  
  
  Q: Why Supertest?
&lt;/h2&gt;

&lt;p&gt;👉 API testing without server&lt;/p&gt;




&lt;h2&gt;
  
  
  Q: Unit vs Integration?
&lt;/h2&gt;

&lt;p&gt;👉 Function vs full flow&lt;/p&gt;




&lt;h1&gt;
  
  
  🚀 19. Advanced Topics
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;TDD&lt;/li&gt;
&lt;li&gt;Load testing&lt;/li&gt;
&lt;li&gt;Security testing&lt;/li&gt;
&lt;li&gt;Contract testing&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  🏁 20. Final Summary
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Jest = test runner
Supertest = API tester
DB memory = safe DB
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  🧠 Final Thought
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;Testing is not about checking code&lt;br&gt;
It is about &lt;strong&gt;preventing failure before users face it&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
    </item>
    <item>
      <title>🚀 Sorting Algorithms in JavaScript (Complete Guide with Examples)</title>
      <dc:creator>Abhishek Gupta </dc:creator>
      <pubDate>Sun, 29 Mar 2026 05:26:15 +0000</pubDate>
      <link>https://forem.com/abhishek-nexgen-dev/sorting-algorithms-in-javascript-complete-guide-with-examples-3gam</link>
      <guid>https://forem.com/abhishek-nexgen-dev/sorting-algorithms-in-javascript-complete-guide-with-examples-3gam</guid>
      <description>&lt;p&gt;Let’s sort this array using different algorithms 👇&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;```js id="arr01"&lt;br&gt;
let nums = [40, 10, 50, 80];&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;


---

## 🔹 1. Bubble Sort

Repeatedly swaps adjacent elements if they are in the wrong order.



```js id="bubble01"
let arr = [...nums];

for (let i = 0; i &amp;lt; arr.length - 1; i++) {
  for (let j = 0; j &amp;lt; arr.length - i - 1; j++) {
    if (arr[j] &amp;gt; arr[j + 1]) {
      let temp = arr[j];
      arr[j] = arr[j + 1];
      arr[j + 1] = temp;
    }
  }
}

console.log(arr);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;✅ Simple but slow (O(n²))&lt;/p&gt;


&lt;h2&gt;
  
  
  🔹 2. Selection Sort
&lt;/h2&gt;

&lt;p&gt;Finds the smallest element and places it at the correct position.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;```js id="selection01"&lt;br&gt;
let arr = [...nums];&lt;/p&gt;

&lt;p&gt;for (let i = 0; i &amp;lt; arr.length - 1; i++) {&lt;br&gt;
  for (let j = i + 1; j &amp;lt; arr.length; j++) {&lt;br&gt;
    if (arr[j] &amp;lt; arr[i]) {&lt;br&gt;
      let temp = arr[i];&lt;br&gt;
      arr[i] = arr[j];&lt;br&gt;
      arr[j] = temp;&lt;br&gt;
    }&lt;br&gt;
  }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;console.log(arr);&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;


✅ Fewer swaps than bubble sort
⚠️ Still O(n²)

---

## 🔹 3. Insertion Sort

Builds the sorted array one element at a time.



```js id="insertion01"
let arr = [...nums];

for (let i = 1; i &amp;lt; arr.length; i++) {
  let key = arr[i];
  let j = i - 1;

  while (j &amp;gt;= 0 &amp;amp;&amp;amp; arr[j] &amp;gt; key) {
    arr[j + 1] = arr[j];
    j--;
  }

  arr[j + 1] = key;
}

console.log(arr);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;✅ Efficient for small or nearly sorted arrays&lt;br&gt;
⏱️ O(n²) worst case&lt;/p&gt;


&lt;h2&gt;
  
  
  🔹 4. Merge Sort (Divide &amp;amp; Conquer)
&lt;/h2&gt;

&lt;p&gt;Breaks the array into halves, sorts them, and merges back.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;```js id="merge01"&lt;br&gt;
function mergeSort(arr) {&lt;br&gt;
  if (arr.length &amp;lt;= 1) return arr;&lt;/p&gt;

&lt;p&gt;let mid = Math.floor(arr.length / 2);&lt;br&gt;
  let left = mergeSort(arr.slice(0, mid));&lt;br&gt;
  let right = mergeSort(arr.slice(mid));&lt;/p&gt;

&lt;p&gt;return merge(left, right);&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;function merge(left, right) {&lt;br&gt;
  let result = [];&lt;br&gt;
  let i = 0, j = 0;&lt;/p&gt;

&lt;p&gt;while (i &amp;lt; left.length &amp;amp;&amp;amp; j &amp;lt; right.length) {&lt;br&gt;
    if (left[i] &amp;lt; right[j]) {&lt;br&gt;
      result.push(left[i]);&lt;br&gt;
      i++;&lt;br&gt;
    } else {&lt;br&gt;
      result.push(right[j]);&lt;br&gt;
      j++;&lt;br&gt;
    }&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;return result.concat(left.slice(i)).concat(right.slice(j));&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;console.log(mergeSort(nums));&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;


✅ Much faster for large datasets
⏱️ O(n log n)
📌 Uses extra space

---

## 🔹 5. Built-in Sort (JavaScript)



```js id="builtin01"
let arr = [...nums];
arr.sort((a, b) =&amp;gt; a - b);

console.log(arr);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;✅ Fast and optimized (recommended in real-world use)&lt;/p&gt;


&lt;h2&gt;
  
  
  🧠 Final Output
&lt;/h2&gt;

&lt;p&gt;All methods will return:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;```js id="out01"&lt;br&gt;
[10, 40, 50, 80]&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;


---

## 💡 Key Takeaways

* Bubble, Selection, Insertion → Easy but slower (O(n²))
* Merge Sort → Efficient (O(n log n)) for large arrays
* Built-in `.sort()` → Best for practical use
* Always understand the logic before memorizing code


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
    </item>
    <item>
      <title>How to Print Alternate Elements of an Array in JavaScript (DSA)</title>
      <dc:creator>Abhishek Gupta </dc:creator>
      <pubDate>Fri, 20 Mar 2026 09:11:10 +0000</pubDate>
      <link>https://forem.com/abhishek-nexgen-dev/how-to-print-alternate-elements-of-an-array-in-javascript-dsa-3k11</link>
      <guid>https://forem.com/abhishek-nexgen-dev/how-to-print-alternate-elements-of-an-array-in-javascript-dsa-3k11</guid>
      <description>&lt;h2&gt;
  
  
  🧩 Problem: Alternate Elements of an Array
&lt;/h2&gt;

&lt;p&gt;Given an array &lt;code&gt;arr[]&lt;/code&gt;, your task is to print every alternate element of the array starting from the &lt;strong&gt;first element&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In other words, print elements at index &lt;code&gt;0, 2, 4, ...&lt;/code&gt; and skip the rest.&lt;/p&gt;




&lt;h3&gt;
  
  
  📥 Input
&lt;/h3&gt;

&lt;p&gt;An array of integers &lt;code&gt;arr[]&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  📤 Output
&lt;/h3&gt;

&lt;p&gt;Print the alternate elements of the array (starting from index 0)&lt;/p&gt;




&lt;h3&gt;
  
  
  🧪 Examples
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: arr[] = [10, 20, 30, 40, 50]
Output: 10 30 50
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;br&gt;
Print the first element (10), skip the second (20), print the third (30), skip the fourth (40), and print the fifth (50).&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: arr[] = [-5, 1, 4, 2, 12]
Output: -5 4 12
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  ⚠️ Constraints
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 ≤ arr.length ≤ 10^5&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-10^9 ≤ arr[i] ≤ 10^9&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Approach 1
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;removeAlternate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;Pointer1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;Pointer2&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;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Pointer2&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;Pointer2&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Pointer2&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&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="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;Pointer1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;Pointer2&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
      &lt;span class="nx"&gt;Pointer1&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Pointer1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;arr&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;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%2Fmqrtl7zrvap6luybgjh3.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%2Fmqrtl7zrvap6luybgjh3.png" alt=" " width="701" height="635"&gt;&lt;/a&gt;&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%2F5skecf0fou9bgkc0wwux.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%2F5skecf0fou9bgkc0wwux.png" alt=" " width="501" height="748"&gt;&lt;/a&gt;&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%2F6gkiqwe75ltjdyepg7j8.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%2F6gkiqwe75ltjdyepg7j8.png" alt=" " width="800" height="226"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Approach 2
&lt;/h3&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%2Fullf7gi79xt1o1qc3ofa.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%2Fullf7gi79xt1o1qc3ofa.png" alt=" " width="788" height="503"&gt;&lt;/a&gt;&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%2Fff4r7lil1o0tkawlfv0o.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%2Fff4r7lil1o0tkawlfv0o.png" alt=" " width="800" height="336"&gt;&lt;/a&gt;&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%2F0hwtgbth3d3u9lzhbb68.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%2F0hwtgbth3d3u9lzhbb68.png" alt=" " width="800" height="453"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;removeAlternate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;Alternate_Arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

  &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&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="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&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="nx"&gt;Alternate_Arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
      &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;Alternate_Arr&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;h3&gt;
  
  
  Approach 3
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;removeAlternate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;Alternate_Arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

  &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&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="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&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="nx"&gt;Alternate_Arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;Alternate_Arr&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;



</description>
      <category>algorithms</category>
      <category>beginners</category>
      <category>computerscience</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Repeated Substring Pattern in JavaScript — Brute Force Approach Explained</title>
      <dc:creator>Abhishek Gupta </dc:creator>
      <pubDate>Sat, 14 Mar 2026 15:06:22 +0000</pubDate>
      <link>https://forem.com/abhishek-nexgen-dev/repeated-substring-pattern-in-javascript-brute-force-approach-explained-3813</link>
      <guid>https://forem.com/abhishek-nexgen-dev/repeated-substring-pattern-in-javascript-brute-force-approach-explained-3813</guid>
      <description>&lt;h1&gt;
  
  
  Problem Statement
&lt;/h1&gt;

&lt;p&gt;Given a string &lt;code&gt;s&lt;/code&gt;, determine whether it can be constructed by repeating a substring of itself.&lt;/p&gt;

&lt;p&gt;Return &lt;strong&gt;true&lt;/strong&gt; if the string can be formed by repeating a substring, otherwise return &lt;strong&gt;false&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  Example
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;abab&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="nx"&gt;Output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="nx"&gt;Explanation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ab&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;repeated&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="nx"&gt;times&lt;/span&gt;

&lt;span class="nx"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;abcabcabc&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="nx"&gt;Output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="nx"&gt;Explanation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;abc&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;repeated&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="nx"&gt;times&lt;/span&gt;

&lt;span class="nx"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;aba&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="nx"&gt;Output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;

&lt;span class="nx"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;aaaa&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="nx"&gt;Output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="nx"&gt;Explanation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;repeated&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="nx"&gt;times&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Understanding the Problem
&lt;/h1&gt;

&lt;p&gt;We need to check if a &lt;strong&gt;smaller substring repeats multiple times to form the entire string&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"abcabcabc"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Possible repeating substring:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"abc"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we repeat &lt;code&gt;"abc"&lt;/code&gt; three times:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"abcabcabc"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This matches the original string, so the answer is &lt;strong&gt;true&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;But consider:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"aba"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is no substring that can repeat to form &lt;code&gt;"aba"&lt;/code&gt;, so the answer is &lt;strong&gt;false&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  Brute Force Approach
&lt;/h1&gt;

&lt;p&gt;The simplest way to solve this problem is to &lt;strong&gt;try every possible substring&lt;/strong&gt; and check if repeating it forms the original string.&lt;/p&gt;

&lt;p&gt;Important observation:&lt;/p&gt;

&lt;p&gt;The repeating substring length must &lt;strong&gt;divide the length of the string&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;String = "abab"
Length = 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Possible substring lengths:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1
2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now test them.&lt;/p&gt;

&lt;p&gt;Substring &lt;code&gt;"a"&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;repeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="err"&gt;→&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;aaaa&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="err"&gt;❌&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Substring &lt;code&gt;"ab"&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ab&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;repeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="err"&gt;→&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;abab&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="err"&gt;✅&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So &lt;code&gt;"ab"&lt;/code&gt; is the repeating substring.&lt;/p&gt;




&lt;h1&gt;
  
  
  Algorithm
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;Get the length of the string.&lt;/li&gt;
&lt;li&gt;Iterate through all possible substring lengths from &lt;code&gt;1&lt;/code&gt; to &lt;code&gt;length / 2&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If the string length is divisible by the substring length:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Extract the substring.&lt;/li&gt;
&lt;li&gt;Repeat it enough times to match the original length.&lt;/li&gt;
&lt;li&gt;Compare it with the original string.

&lt;ol&gt;
&lt;li&gt;If a match is found → return &lt;code&gt;true&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Otherwise return &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  JavaScript Implementation
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;repeatedSubstringPattern&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;len&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;len&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="nx"&gt;i&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="p"&gt;{&lt;/span&gt;

            &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;sub&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sub&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;repeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;len&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&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;h1&gt;
  
  
  Testing the Solution
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;repeatedSubstringPattern&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;abcabcabcabc&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; 
&lt;span class="c1"&gt;// true&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;repeatedSubstringPattern&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;aba&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; 
&lt;span class="c1"&gt;// false&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;repeatedSubstringPattern&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;abab&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; 
&lt;span class="c1"&gt;// true&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;repeatedSubstringPattern&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;aaaa&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; 
&lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Dry Run Example
&lt;/h1&gt;

&lt;p&gt;Let’s test the string:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"ababab"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Length:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Possible substring lengths:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1, 2, 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Check each one.&lt;/p&gt;

&lt;p&gt;Substring &lt;code&gt;"a"&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;repeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="err"&gt;→&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;aaaaaa&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="err"&gt;❌&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Substring &lt;code&gt;"ab"&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ab&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;repeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="err"&gt;→&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ababab&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="err"&gt;✅&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the function returns &lt;strong&gt;true&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  Time Complexity
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;O(n²)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because we test multiple substring lengths and perform string comparisons.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>The Complete Guide to Working With AI While Coding (Hackathons, Cursor, VS Code, AI Agents)</title>
      <dc:creator>Abhishek Gupta </dc:creator>
      <pubDate>Tue, 10 Mar 2026 12:37:23 +0000</pubDate>
      <link>https://forem.com/abhishek-nexgen-dev/the-complete-guide-to-working-with-ai-while-coding-hackathons-cursor-vs-code-ai-agents-3821</link>
      <guid>https://forem.com/abhishek-nexgen-dev/the-complete-guide-to-working-with-ai-while-coding-hackathons-cursor-vs-code-ai-agents-3821</guid>
      <description>&lt;p&gt;AI has fundamentally changed how developers build software.&lt;/p&gt;

&lt;p&gt;In hackathons, startups, and daily development, many developers now rely on tools like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cursor&lt;/li&gt;
&lt;li&gt;VS Code + GitHub Copilot&lt;/li&gt;
&lt;li&gt;ChatGPT / Claude&lt;/li&gt;
&lt;li&gt;Aider&lt;/li&gt;
&lt;li&gt;Continue&lt;/li&gt;
&lt;li&gt;Antigravity agents&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These tools can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;generate code&lt;/li&gt;
&lt;li&gt;modify multiple files&lt;/li&gt;
&lt;li&gt;refactor large parts of a project&lt;/li&gt;
&lt;li&gt;debug errors&lt;/li&gt;
&lt;li&gt;suggest architecture&lt;/li&gt;
&lt;li&gt;write tests&lt;/li&gt;
&lt;li&gt;even build full features&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because of this, &lt;strong&gt;small teams can now build large systems extremely fast&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;But there is a hidden problem.&lt;/p&gt;

&lt;p&gt;Many developers use AI &lt;strong&gt;without a system&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;At first everything feels amazing.&lt;br&gt;
AI generates code quickly and features appear fast.&lt;/p&gt;

&lt;p&gt;But after some time, the project starts breaking.&lt;/p&gt;

&lt;p&gt;You begin to notice things like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;messy code structure&lt;/li&gt;
&lt;li&gt;features randomly breaking&lt;/li&gt;
&lt;li&gt;AI modifying unrelated files&lt;/li&gt;
&lt;li&gt;duplicate logic appearing&lt;/li&gt;
&lt;li&gt;debugging becoming harder and harder&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This happens because developers &lt;strong&gt;don’t manage AI collaboration properly&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The correct approach is simple:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Treat AI like a developer on your team.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If a new developer joins your project, you don’t just say:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Build whatever you want.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You give them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;documentation&lt;/li&gt;
&lt;li&gt;architecture explanation&lt;/li&gt;
&lt;li&gt;coding rules&lt;/li&gt;
&lt;li&gt;feature plans&lt;/li&gt;
&lt;li&gt;debugging notes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;AI needs the &lt;strong&gt;same structure&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This guide explains how to build a &lt;strong&gt;proper system for working with AI while coding&lt;/strong&gt;.&lt;/p&gt;


&lt;h1&gt;
  
  
  Why AI Sometimes Breaks Your Project
&lt;/h1&gt;

&lt;p&gt;AI tools work with &lt;strong&gt;limited context&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Most coding assistants only see:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the file you opened&lt;/li&gt;
&lt;li&gt;a few related files&lt;/li&gt;
&lt;li&gt;your conversation with the AI&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They &lt;strong&gt;do not automatically understand your entire project&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Imagine your project has:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;controllers&lt;/li&gt;
&lt;li&gt;services&lt;/li&gt;
&lt;li&gt;repositories&lt;/li&gt;
&lt;li&gt;event systems&lt;/li&gt;
&lt;li&gt;socket communication&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If AI doesn’t understand those rules, it may generate code that &lt;strong&gt;breaks your architecture&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;business logic placed inside controllers&lt;/li&gt;
&lt;li&gt;duplicated services&lt;/li&gt;
&lt;li&gt;incorrect database queries&lt;/li&gt;
&lt;li&gt;conflicting APIs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The result is &lt;strong&gt;architecture drift&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;To prevent this, developers must provide &lt;strong&gt;structured context&lt;/strong&gt; for AI.&lt;/p&gt;


&lt;h1&gt;
  
  
  The Solution: Build an AI Workspace
&lt;/h1&gt;

&lt;p&gt;The best way to work with AI is to treat your project as having &lt;strong&gt;two systems&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;1️⃣ Your &lt;strong&gt;application code&lt;/strong&gt;&lt;br&gt;
2️⃣ Your &lt;strong&gt;AI collaboration system&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A clean structure might look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;project-root/

README.md
AI_RULES.md

docs/
plans/
ai-logs/
debug/
reviews/
ai-history/

src/
tests/
scripts/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This structure helps both:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;human developers&lt;/li&gt;
&lt;li&gt;AI coding tools&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;understand the project.&lt;/p&gt;




&lt;h1&gt;
  
  
  Step 1: Write a Clear README
&lt;/h1&gt;

&lt;p&gt;Many AI tools read the &lt;strong&gt;README first&lt;/strong&gt; to understand the project.&lt;/p&gt;

&lt;p&gt;A good README should explain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;what the project does&lt;/li&gt;
&lt;li&gt;the technology stack&lt;/li&gt;
&lt;li&gt;the main modules&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="gh"&gt;# CommDesk&lt;/span&gt;

CommDesk is a platform for managing developer communities,
events, and hackathons.

Stack

Frontend: React + Vite
Backend: Node.js + Express
Database: MongoDB
Desktop: Tauri
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This gives AI &lt;strong&gt;immediate context about your system&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  Step 2: Create AI Rules
&lt;/h1&gt;

&lt;p&gt;Create a file called:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AI_RULES.md
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This file tells AI &lt;strong&gt;how your codebase works&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AI Rules

1. Controllers only handle HTTP requests.
2. Business logic must stay inside services.
3. Database queries belong in repositories.
4. Follow the existing folder structure.
5. Do not duplicate existing logic.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This prevents AI from &lt;strong&gt;breaking your architecture&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  Step 3: Create a Documentation Folder
&lt;/h1&gt;

&lt;p&gt;Create a folder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docs/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docs/
AI_CONTEXT.md
ARCHITECTURE.md
FILE_STRUCTURE.md
CODING_GUIDELINES.md
SYSTEM_FLOW.md
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These documents provide &lt;strong&gt;project-level understanding&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  AI_CONTEXT.md
&lt;/h3&gt;

&lt;p&gt;Explain what the project does.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Project Overview

CommDesk is a platform for managing developer communities.

Core modules:
- Authentication
- Event Management
- Notification System
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  ARCHITECTURE.md
&lt;/h3&gt;

&lt;p&gt;Explain the system design.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client → API → Services → Database

Services:

Auth Service
Event Service
Notification Service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  FILE_STRUCTURE.md
&lt;/h3&gt;

&lt;p&gt;Explain how the codebase is organized.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;src/

controllers/
services/
models/
routes/
utils/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This prevents AI from inventing &lt;strong&gt;new random structures&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  Step 4: Plan Features Before Asking AI
&lt;/h1&gt;

&lt;p&gt;Instead of telling AI:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Build notifications.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Create a plan first.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;plans/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;IMPLEMENTATION_PLAN.md
FUTURE_FEATURES.md
ROADMAP.md
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example feature plan:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Feature: Notification System

Step 1 Create database schema
Step 2 Create service layer
Step 3 Create API endpoints
Step 4 Add socket events
Step 5 Create UI components
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now AI can &lt;strong&gt;build features step by step&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  Step 5: Track AI Changes
&lt;/h1&gt;

&lt;p&gt;Create a folder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ai-logs/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CHANGE_LOG.md
PROMPTS.md
AI_SESSIONS.md
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example log:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Date: 2026-03-10
AI Tool: Cursor

Task:
Create Notification Service

Files Created:
src/services/notificationService.js

Files Updated:
src/controllers/notificationController.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Tracking AI changes makes debugging &lt;strong&gt;much easier later&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  Step 6: Store AI Session History
&lt;/h1&gt;

&lt;p&gt;Create:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ai-history/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ai-history/
2026/
 03/
  10/
   session-1.md
   session-2.md
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example session log:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Date: 2026-03-10
Time: 15:30

AI Tool: Cursor

Task:
Create notification service

Files created:
notificationService.js

Developer Notes:
Added validation manually.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This becomes a &lt;strong&gt;timeline of how the project evolved&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  Step 7: Track Bugs and Fixes
&lt;/h1&gt;

&lt;p&gt;Create a folder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;debug/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ERRORS.md
FIXES.md
INCIDENTS.md
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Error

Notification event not triggering
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cause:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Incorrect socket event name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fix:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Changed event name
notifyUser → notification:new
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This builds a &lt;strong&gt;knowledge base of problems and solutions&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  Step 8: Review AI Code
&lt;/h1&gt;

&lt;p&gt;AI-generated code should always be reviewed.&lt;/p&gt;

&lt;p&gt;Create:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;reviews/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AI_REVIEW.md
CODE_REVIEW.md
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example review:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;File
notificationService.js

Issues
Missing validation
Missing error handling
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This keeps your codebase &lt;strong&gt;clean and maintainable&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  Example AI Development Workflow
&lt;/h1&gt;

&lt;p&gt;A typical workflow might look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Feature Idea
      ↓
Write Implementation Plan
      ↓
Ask AI to generate step 1
      ↓
Review generated code
      ↓
Log AI changes
      ↓
Test the feature
      ↓
Fix bugs with AI
      ↓
Update documentation
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Example Bug Fix Workflow
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Bug detected
      ↓
Record in debug/ERRORS.md
      ↓
Ask AI to investigate
      ↓
Apply fix
      ↓
Log the change
      ↓
Document the solution
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Best Practices When Using AI
&lt;/h1&gt;

&lt;p&gt;Always:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ask AI for &lt;strong&gt;small tasks&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;review generated code carefully&lt;/li&gt;
&lt;li&gt;track AI-generated changes&lt;/li&gt;
&lt;li&gt;maintain documentation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Avoid:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;generating entire systems in one prompt&lt;/li&gt;
&lt;li&gt;blindly trusting AI output&lt;/li&gt;
&lt;li&gt;skipping architecture planning&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  The Most Important Skill in the AI Era
&lt;/h1&gt;

&lt;p&gt;Many developers believe the key skill today is &lt;strong&gt;prompt engineering&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;But the real skill is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Context Engineering&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Context engineering means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;organizing documentation&lt;/li&gt;
&lt;li&gt;structuring your project clearly&lt;/li&gt;
&lt;li&gt;giving AI the right information&lt;/li&gt;
&lt;li&gt;guiding AI step by step&lt;/li&gt;
&lt;li&gt;maintaining development history&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Developers who master this can build &lt;strong&gt;faster, more reliable software with AI&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  Final Thoughts
&lt;/h1&gt;

&lt;p&gt;AI tools like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cursor&lt;/li&gt;
&lt;li&gt;Copilot&lt;/li&gt;
&lt;li&gt;VS Code AI extensions&lt;/li&gt;
&lt;li&gt;Antigravity&lt;/li&gt;
&lt;li&gt;AI coding agents&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;are transforming software development.&lt;/p&gt;

&lt;p&gt;But AI works best when it is &lt;strong&gt;guided properly&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When developers provide:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;clear documentation&lt;/li&gt;
&lt;li&gt;structured plans&lt;/li&gt;
&lt;li&gt;coding rules&lt;/li&gt;
&lt;li&gt;review processes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;AI becomes &lt;strong&gt;a powerful development partner&lt;/strong&gt; instead of a source of chaos.&lt;/p&gt;

&lt;p&gt;And learning how to collaborate with AI will likely become one of the &lt;strong&gt;most important skills for developers in the coming years&lt;/strong&gt;.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>🧠 Sorting Algorithms Practice (Level 0 Hero)</title>
      <dc:creator>Abhishek Gupta </dc:creator>
      <pubDate>Sun, 08 Mar 2026 07:34:44 +0000</pubDate>
      <link>https://forem.com/abhishek-nexgen-dev/sorting-algorithms-practice-level-0-hero-1h8e</link>
      <guid>https://forem.com/abhishek-nexgen-dev/sorting-algorithms-practice-level-0-hero-1h8e</guid>
      <description>&lt;h2&gt;
  
  
  Part 1 — Beginner Level (Problems 0–10)
&lt;/h2&gt;

&lt;p&gt;Sorting is one of the &lt;strong&gt;core foundations of Data Structures and Algorithms&lt;/strong&gt;. Many interview questions become easier after sorting the data.&lt;/p&gt;

&lt;h1&gt;
  
  
  Problem 0 — Sort Three Numbers
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Difficulty
&lt;/h2&gt;

&lt;p&gt;Easy&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;You are given &lt;strong&gt;three integers&lt;/strong&gt; &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;b&lt;/code&gt;, and &lt;code&gt;c&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Your task is to return the numbers in &lt;strong&gt;ascending order&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The result should be returned as an array or list.&lt;/p&gt;




&lt;h2&gt;
  
  
  Input
&lt;/h2&gt;

&lt;p&gt;Three integers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a = 7
b = 2
c = 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[2,5,7]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Example 1
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input
a = 10
b = 3
c = 6

Output
[3,6,10]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;The numbers sorted in ascending order are:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3 &amp;lt; 6 &amp;lt; 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-10^6 ≤ a,b,c ≤ 10^6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Hint
&lt;/h2&gt;

&lt;p&gt;You can solve this by:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;comparing numbers manually&lt;/li&gt;
&lt;li&gt;using a small sorting algorithm&lt;/li&gt;
&lt;li&gt;using built-in sort function&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Problem 1 — Sort an Integer Array
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Difficulty
&lt;/h2&gt;

&lt;p&gt;Easy&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Given an integer array &lt;code&gt;nums&lt;/code&gt;, sort the array in &lt;strong&gt;ascending order&lt;/strong&gt; and return it.&lt;/p&gt;

&lt;p&gt;You may use any sorting algorithm such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Bubble Sort&lt;/li&gt;
&lt;li&gt;Merge Sort&lt;/li&gt;
&lt;li&gt;Quick Sort&lt;/li&gt;
&lt;li&gt;Heap Sort&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Input
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nums = [5,2,3,1]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1,2,3,5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Example 2
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input
nums = [5,1,1,2,0,0]

Output
[0,0,1,1,2,5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 ≤ nums.length ≤ 50000
-50000 ≤ nums[i] ≤ 50000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;Sorting the numbers in ascending order means:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;smallest → largest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Original:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;5,2,3,1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sorted:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1,2,3,5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Hint
&lt;/h2&gt;

&lt;p&gt;Efficient algorithms:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Merge Sort  → O(n log n)
Quick Sort  → O(n log n)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Problem 2 — Sort Array in Descending Order
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Difficulty
&lt;/h2&gt;

&lt;p&gt;Easy&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Given an array of integers &lt;code&gt;nums&lt;/code&gt;, sort the array in &lt;strong&gt;descending order&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Input
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nums = [1,5,3,2]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[5,3,2,1]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Example 2
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input
nums = [10,4,7,1]

Output
[10,7,4,1]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;Descending order means:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;largest → smallest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 ≤ nums.length ≤ 10^5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Hint
&lt;/h2&gt;

&lt;p&gt;You can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;sort normally&lt;/li&gt;
&lt;li&gt;reverse the result&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Problem 3 — Check if Array is Sorted
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Difficulty
&lt;/h2&gt;

&lt;p&gt;Easy&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Given an array &lt;code&gt;nums&lt;/code&gt;, determine whether the array is sorted in &lt;strong&gt;non-decreasing order&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Return:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;true  → if sorted
false → otherwise
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Input
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nums = [1,2,3,4,5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Example 2
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input
nums = [1,3,2]

Output
false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;The second example fails because:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3 &amp;gt; 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the array is &lt;strong&gt;not sorted&lt;/strong&gt;.&lt;/p&gt;




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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 ≤ nums.length ≤ 10^5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Hint
&lt;/h2&gt;

&lt;p&gt;Loop through the array and check:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nums[i] ≤ nums[i+1]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Problem 4 — Bubble Sort Implementation
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Difficulty
&lt;/h2&gt;

&lt;p&gt;Easy&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Implement the &lt;strong&gt;Bubble Sort algorithm&lt;/strong&gt; to sort an array in ascending order.&lt;/p&gt;

&lt;p&gt;Bubble Sort repeatedly swaps adjacent elements if they are in the wrong order.&lt;/p&gt;




&lt;h2&gt;
  
  
  Input
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nums = [5,1,4,2]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1,2,4,5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;Original array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;5 1 4 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pass 1&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 5 4 2
1 4 5 2
1 4 2 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pass 2&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 4 2 5
1 2 4 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sorted array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 2 4 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 ≤ nums.length ≤ 1000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Time Complexity
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;O(n²)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Problem 5 — Selection Sort Implementation
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Difficulty
&lt;/h2&gt;

&lt;p&gt;Easy&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Implement &lt;strong&gt;Selection Sort&lt;/strong&gt; to sort the array.&lt;/p&gt;

&lt;p&gt;Selection Sort works by repeatedly selecting the &lt;strong&gt;minimum element&lt;/strong&gt; from the unsorted part and placing it at the beginning.&lt;/p&gt;




&lt;h2&gt;
  
  
  Input
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nums = [64,25,12,22,11]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[11,12,22,25,64]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;Steps&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;11 25 12 22 64
11 12 25 22 64
11 12 22 25 64
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Time Complexity
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;O(n²)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Problem 6 — Insertion Sort Implementation
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Difficulty
&lt;/h2&gt;

&lt;p&gt;Easy&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Sort the array using &lt;strong&gt;Insertion Sort&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Insertion sort builds the sorted array &lt;strong&gt;one element at a time&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Input
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nums = [8,3,5,2]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[2,3,5,8]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;Steps:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;8
3 8
3 5 8
2 3 5 8
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Time Complexity
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Best Case  → O(n)
Worst Case → O(n²)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Problem 7 — Move Zeros to End
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Difficulty
&lt;/h2&gt;

&lt;p&gt;Easy&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Given an array &lt;code&gt;nums&lt;/code&gt;, move all &lt;code&gt;0&lt;/code&gt;s to the &lt;strong&gt;end of the array&lt;/strong&gt; while maintaining the &lt;strong&gt;relative order of non-zero elements&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Input
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nums = [0,1,0,3,12]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1,3,12,0,0]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;Original&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0 1 0 3 12
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Move zeros&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 3 12 0 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 ≤ nums.length ≤ 10^4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Problem 8 — Sort Even and Odd Numbers
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Difficulty
&lt;/h2&gt;

&lt;p&gt;Easy&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Rearrange the array so that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;All even numbers appear first
All odd numbers appear after them
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The relative order does not matter.&lt;/p&gt;




&lt;h2&gt;
  
  
  Input
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nums = [3,1,2,4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[2,4,3,1]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;Even numbers&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Odd numbers&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Combined&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2 4 3 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Problem 9 — Sort 0s and 1s
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Difficulty
&lt;/h2&gt;

&lt;p&gt;Easy&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Given a binary array containing only &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;1&lt;/code&gt;, sort the array.&lt;/p&gt;




&lt;h2&gt;
  
  
  Input
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nums = [1,0,1,0,1]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[0,0,1,1,1]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Hint
&lt;/h2&gt;

&lt;p&gt;Count number of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;zeros
ones
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Problem 10 — Sort Array by Absolute Value
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Difficulty
&lt;/h2&gt;

&lt;p&gt;Easy&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Sort the array based on the &lt;strong&gt;absolute value of numbers&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Input
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nums = [-4,-1,0,3,10]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[0,-1,3,-4,10]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;Absolute values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0 → 0
-1 → 1
3 → 3
-4 → 4
10 → 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sorted by absolute value.&lt;/p&gt;

&lt;h1&gt;
  
  
  Part 2 — Intermediate Level (Problems 11–20)
&lt;/h1&gt;

&lt;p&gt;These problems introduce &lt;strong&gt;real interview patterns&lt;/strong&gt; where sorting is used with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Two pointers&lt;/li&gt;
&lt;li&gt;Hash maps&lt;/li&gt;
&lt;li&gt;Custom comparators&lt;/li&gt;
&lt;li&gt;Frequency counting&lt;/li&gt;
&lt;li&gt;Merging sorted data&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Problem 11 — Sort Colors (Dutch National Flag Problem)
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Medium&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;You are given an array &lt;code&gt;nums&lt;/code&gt; containing &lt;code&gt;n&lt;/code&gt; objects colored &lt;strong&gt;red, white, or blue&lt;/strong&gt;, represented by the integers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0 → red  
1 → white  
2 → blue
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sort the array &lt;strong&gt;in-place&lt;/strong&gt; so that objects of the same color are adjacent in the order:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0 → 1 → 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You must solve the problem &lt;strong&gt;without using the built-in sort function&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Input
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nums = [2,0,2,1,1,0]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[0,0,1,1,2,2]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Example 2
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input
nums = [2,0,1]

Output
[0,1,2]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 ≤ nums.length ≤ 300
nums[i] ∈ {0,1,2}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;The sorted order should be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0s → first
1s → middle
2s → last
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Hint
&lt;/h2&gt;

&lt;p&gt;Use the &lt;strong&gt;Dutch National Flag Algorithm&lt;/strong&gt; with &lt;strong&gt;three pointers&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;low
mid
high
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Time Complexity:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;O(n)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Problem 12 — Merge Two Sorted Arrays
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Easy&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;You are given &lt;strong&gt;two sorted arrays&lt;/strong&gt; &lt;code&gt;nums1&lt;/code&gt; and &lt;code&gt;nums2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Merge &lt;code&gt;nums2&lt;/code&gt; into &lt;code&gt;nums1&lt;/code&gt; as &lt;strong&gt;one sorted array&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Input
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nums1 = [1,2,3]
nums2 = [2,5,6]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1,2,2,3,5,6]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Example 2
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input
nums1 = [1,3,5]
nums2 = [2,4,6]

Output
[1,2,3,4,5,6]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 ≤ nums1.length, nums2.length ≤ 1000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;Because both arrays are already sorted, we can merge them similar to the &lt;strong&gt;Merge step in Merge Sort&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Hint
&lt;/h2&gt;

&lt;p&gt;Use &lt;strong&gt;two pointers&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;i → nums1
j → nums2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Compare elements and insert the smaller one.&lt;/p&gt;




&lt;h1&gt;
  
  
  Problem 13 — Intersection of Two Sorted Arrays
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Easy&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Given &lt;strong&gt;two sorted arrays&lt;/strong&gt;, return their &lt;strong&gt;intersection&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The intersection should contain &lt;strong&gt;common elements appearing in both arrays&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Input
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nums1 = [1,2,2,3]
nums2 = [2,2]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[2,2]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Example 2
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input
nums1 = [1,3,4,5]
nums2 = [2,3,5,7]

Output
[3,5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 ≤ nums1.length, nums2.length ≤ 1000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Hint
&lt;/h2&gt;

&lt;p&gt;Use &lt;strong&gt;two pointers&lt;/strong&gt; to scan both arrays.&lt;/p&gt;




&lt;h1&gt;
  
  
  Problem 14 — Union of Two Sorted Arrays
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Easy&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Given two sorted arrays, return the &lt;strong&gt;union of the arrays&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The union should contain &lt;strong&gt;all unique elements from both arrays&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Input
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nums1 = [1,2,4]
nums2 = [2,3,5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1,2,3,4,5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Example 2
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input
nums1 = [1,1,2,3]
nums2 = [2,3,4]

Output
[1,2,3,4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Hint
&lt;/h2&gt;

&lt;p&gt;Use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;two pointers
set
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Problem 15 — Sort Array by Frequency
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Medium&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Given an integer array &lt;code&gt;nums&lt;/code&gt;, sort the array based on the &lt;strong&gt;frequency of values&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Rules:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Numbers with &lt;strong&gt;lower frequency come first&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;If frequencies are equal, &lt;strong&gt;larger numbers appear first&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Input
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nums = [2,3,1,3,2]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1,3,3,2,2]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;Frequencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 → 1 time
3 → 2 times
2 → 2 times
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because &lt;code&gt;3&lt;/code&gt; and &lt;code&gt;2&lt;/code&gt; appear the same number of times, the &lt;strong&gt;larger number comes first&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Hint
&lt;/h2&gt;

&lt;p&gt;Steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Count frequencies using a hashmap&lt;/li&gt;
&lt;li&gt;Sort using a custom comparator&lt;/li&gt;
&lt;/ol&gt;




&lt;h1&gt;
  
  
  Problem 16 — Relative Sort Array
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Medium&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;You are given two arrays:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr1
arr2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Elements in &lt;code&gt;arr2&lt;/code&gt; are unique and appear in &lt;code&gt;arr1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Sort &lt;code&gt;arr1&lt;/code&gt; such that:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Elements in &lt;code&gt;arr2&lt;/code&gt; appear first&lt;/li&gt;
&lt;li&gt;Their order matches &lt;code&gt;arr2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Remaining elements appear in &lt;strong&gt;ascending order&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Input
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr1 = [2,3,1,3,2,4,6,7,9,2,19]
arr2 = [2,1,4,3,9,6]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[2,2,2,1,4,3,3,9,6,7,19]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Hint
&lt;/h2&gt;

&lt;p&gt;Use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HashMap + Sorting
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Problem 17 — Sort Characters by Frequency
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Medium&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Given a string &lt;code&gt;s&lt;/code&gt;, sort it in &lt;strong&gt;descending order based on character frequency&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Input
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;s = "tree"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"eert"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;Frequency:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;e → 2
t → 1
r → 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result starts with the most frequent character.&lt;/p&gt;




&lt;h2&gt;
  
  
  Hint
&lt;/h2&gt;

&lt;p&gt;Steps:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;count frequency
sort by frequency
rebuild string
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Problem 18 — Sort Matrix Diagonally
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Medium&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Given a matrix &lt;code&gt;mat&lt;/code&gt;, sort &lt;strong&gt;each diagonal in ascending order&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Input
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mat =
[
[3,3,1],
[2,2,1],
[1,1,1]
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
[1,1,1],
[1,2,2],
[1,2,3]
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;Each diagonal must be sorted independently.&lt;/p&gt;




&lt;h2&gt;
  
  
  Hint
&lt;/h2&gt;

&lt;p&gt;Use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HashMap&amp;lt;diagonal, values&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then sort each list.&lt;/p&gt;




&lt;h1&gt;
  
  
  Problem 19 — Sort Linked List
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Medium&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Given the &lt;strong&gt;head of a linked list&lt;/strong&gt;, sort it in ascending order.&lt;/p&gt;




&lt;h2&gt;
  
  
  Input
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;4 → 2 → 1 → 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 → 2 → 3 → 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0 ≤ nodes ≤ 50,000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Hint
&lt;/h2&gt;

&lt;p&gt;Best solution:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Merge Sort on Linked List
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Time Complexity:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;O(n log n)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Problem 20 — Sort People by Height
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Easy&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;You are given two arrays:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;names
heights
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sort the people by &lt;strong&gt;heights in descending order&lt;/strong&gt; and return the names.&lt;/p&gt;




&lt;h2&gt;
  
  
  Input
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;names = ["Mary","John","Emma"]
heights = [180,165,170]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;["Mary","Emma","John"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;Sorted by height:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;180 → Mary
170 → Emma
165 → John
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Great — let’s continue the series.&lt;br&gt;
Below is &lt;strong&gt;Part 3: Problems 21–30&lt;/strong&gt; in a &lt;strong&gt;LeetCode-style detailed format&lt;/strong&gt;, moving toward &lt;strong&gt;advanced sorting patterns&lt;/strong&gt; such as &lt;strong&gt;QuickSelect, inversion counting, and swap optimization&lt;/strong&gt;.&lt;/p&gt;


&lt;h1&gt;
  
  
  Part 3 — Upper-Intermediate Level (Problems 21–30)
&lt;/h1&gt;

&lt;p&gt;These problems combine &lt;strong&gt;sorting with algorithmic techniques&lt;/strong&gt; like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Greedy strategies&lt;/li&gt;
&lt;li&gt;Divide and conquer&lt;/li&gt;
&lt;li&gt;QuickSelect&lt;/li&gt;
&lt;li&gt;Heap / priority queue&lt;/li&gt;
&lt;li&gt;Mathematical observations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These appear frequently in &lt;strong&gt;coding interviews&lt;/strong&gt;.&lt;/p&gt;


&lt;h1&gt;
  
  
  Problem 21 — Kth Largest Element in an Array
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Medium&lt;/p&gt;
&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Given an integer array &lt;code&gt;nums&lt;/code&gt; and an integer &lt;code&gt;k&lt;/code&gt;, return the &lt;strong&gt;kth largest element&lt;/strong&gt; in the array.&lt;/p&gt;

&lt;p&gt;Note:&lt;br&gt;
The kth largest element is the element that would appear in position &lt;code&gt;k&lt;/code&gt; if the array were sorted in &lt;strong&gt;descending order&lt;/strong&gt;.&lt;/p&gt;


&lt;h2&gt;
  
  
  Input
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nums = [3,2,1,5,6,4]
k = 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Example 2
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input
nums = [3,2,3,1,2,4,5,5,6]
k = 4

Output
4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 ≤ k ≤ nums.length ≤ 100000
-10^4 ≤ nums[i] ≤ 10^4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;Sorted descending:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[6,5,4,3,2,1]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2nd largest element = &lt;strong&gt;5&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Hint
&lt;/h2&gt;

&lt;p&gt;Efficient solutions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;QuickSelect
Heap
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Time Complexity:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Average → O(n)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Problem 22 — Kth Smallest Element in an Array
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Medium&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Given an array &lt;code&gt;nums&lt;/code&gt; and an integer &lt;code&gt;k&lt;/code&gt;, return the &lt;strong&gt;kth smallest element&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Input
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nums = [7,10,4,3,20,15]
k = 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;Sorted array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[3,4,7,10,15,20]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3rd smallest = &lt;strong&gt;7&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Hint
&lt;/h2&gt;

&lt;p&gt;Use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;QuickSelect
Min Heap
Sorting
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Problem 23 — Minimum Swaps to Sort Array
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Medium&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Given an array of &lt;strong&gt;distinct integers&lt;/strong&gt;, determine the &lt;strong&gt;minimum number of swaps required&lt;/strong&gt; to sort the array.&lt;/p&gt;




&lt;h2&gt;
  
  
  Input
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nums = [4,3,2,1]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;Initial:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;4 3 2 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Swap:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;4 ↔ 1
3 ↔ 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sorted:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 2 3 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Total swaps = &lt;strong&gt;2&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Hint
&lt;/h2&gt;

&lt;p&gt;Use &lt;strong&gt;cycle detection in permutation&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Time Complexity:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;O(n log n)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Problem 24 — Count Inversions in Array
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Medium&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;An &lt;strong&gt;inversion&lt;/strong&gt; occurs when:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;i &amp;lt; j
nums[i] &amp;gt; nums[j]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your task is to count the &lt;strong&gt;total number of inversions&lt;/strong&gt; in the array.&lt;/p&gt;




&lt;h2&gt;
  
  
  Input
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nums = [2,4,1,3,5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;Inversions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(2,1)
(4,1)
(4,3)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Total = &lt;strong&gt;3&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Hint
&lt;/h2&gt;

&lt;p&gt;Efficient solution:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Merge Sort modification
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Time Complexity:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;O(n log n)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Problem 25 — Pancake Sorting
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Medium&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;You are given an array &lt;code&gt;arr&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In one operation you can &lt;strong&gt;flip the first k elements&lt;/strong&gt; of the array.&lt;/p&gt;

&lt;p&gt;Return a sequence of flips that sorts the array.&lt;/p&gt;




&lt;h2&gt;
  
  
  Input
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr = [3,2,4,1]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[4,2,4,3]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;Flip operations reverse prefixes until array becomes sorted.&lt;/p&gt;




&lt;h2&gt;
  
  
  Hint
&lt;/h2&gt;

&lt;p&gt;Idea:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Bring largest element to front
Then flip to its correct position
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Problem 26 — Wiggle Sort
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Medium&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Reorder the array such that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nums[0] ≤ nums[1] ≥ nums[2] ≤ nums[3] ≥ nums[4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Input
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nums = [3,5,2,1,6,4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[3,5,1,6,2,4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;Check the pattern:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3 ≤ 5 ≥ 1 ≤ 6 ≥ 2 ≤ 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Hint
&lt;/h2&gt;

&lt;p&gt;Sort first then rearrange or use &lt;strong&gt;single pass swapping&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  Problem 27 — Wiggle Sort II
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Medium/Hard&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Rearrange numbers so that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nums[0] &amp;lt; nums[1] &amp;gt; nums[2] &amp;lt; nums[3] &amp;gt; nums[4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Input
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nums = [1,5,1,1,6,4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1,6,1,5,1,4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Hint
&lt;/h2&gt;

&lt;p&gt;Steps:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Find median
Three-way partition
Rearrange
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Problem 28 — Maximum Gap
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Hard&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Given an integer array &lt;code&gt;nums&lt;/code&gt;, find the &lt;strong&gt;maximum difference between two successive elements&lt;/strong&gt; in the sorted form of the array.&lt;/p&gt;

&lt;p&gt;You must solve it in &lt;strong&gt;linear time&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Input
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nums = [3,6,9,1]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;Sorted array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1,3,6,9]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Differences:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2,3,3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Maximum = &lt;strong&gt;3&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Hint
&lt;/h2&gt;

&lt;p&gt;Use &lt;strong&gt;Bucket Sort / Pigeonhole Principle&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  Problem 29 — Top K Frequent Elements
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Medium&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Given an integer array &lt;code&gt;nums&lt;/code&gt; and an integer &lt;code&gt;k&lt;/code&gt;, return the &lt;strong&gt;k most frequent elements&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Input
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nums = [1,1,1,2,2,3]
k = 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1,2]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;Frequency:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 → 3 times
2 → 2 times
3 → 1 time
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Top 2 frequent elements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1,2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Hint
&lt;/h2&gt;

&lt;p&gt;Possible approaches:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Heap
Bucket Sort
QuickSelect
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Problem 30 — Sort Nearly Sorted Array
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Medium&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;An array is &lt;strong&gt;k-sorted&lt;/strong&gt; if every element is at most &lt;code&gt;k&lt;/code&gt; positions away from its sorted position.&lt;/p&gt;

&lt;p&gt;Sort the array efficiently.&lt;/p&gt;




&lt;h2&gt;
  
  
  Input
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nums = [6,5,3,2,8,10,9]
k = 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[2,3,5,6,8,9,10]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Hint
&lt;/h2&gt;

&lt;p&gt;Use a &lt;strong&gt;Min Heap of size k+1&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Time Complexity:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;O(n log k)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below is &lt;strong&gt;Part 4&lt;/strong&gt; of the &lt;strong&gt;Sorting DSA Series (LeetCode Style)&lt;/strong&gt;.&lt;br&gt;
These problems move from &lt;strong&gt;advanced → interview level&lt;/strong&gt; and involve &lt;strong&gt;Quick Sort ideas, custom sorting, and greedy sorting logic&lt;/strong&gt;.&lt;/p&gt;


&lt;h1&gt;
  
  
  🧠 Sorting Problems (Hero Level) — Part 4
&lt;/h1&gt;


&lt;h1&gt;
  
  
  16. Maximum Gap
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Hard&lt;br&gt;
&lt;strong&gt;Concepts:&lt;/strong&gt; Sorting, Bucket Sort, Pigeonhole Principle&lt;/p&gt;


&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;Given an integer array &lt;code&gt;nums&lt;/code&gt;, return the &lt;strong&gt;maximum difference between two successive elements&lt;/strong&gt; in its &lt;strong&gt;sorted form&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If the array contains less than two elements, return &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You must solve it in &lt;strong&gt;linear time and space&lt;/strong&gt; if possible.&lt;/p&gt;


&lt;h2&gt;
  
  
  Example 1
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input:
nums = [3,6,9,1]

Output:
3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Explanation
&lt;/h3&gt;

&lt;p&gt;Sorted array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1,3,6,9]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Differences:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3-1 = 2
6-3 = 3
9-6 = 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Maximum gap = &lt;strong&gt;3&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Example 2
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input:
nums = [10]

Output:
0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation
&lt;/h3&gt;

&lt;p&gt;Only one element → no gap exists.&lt;/p&gt;




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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 ≤ nums.length ≤ 10^5
0 ≤ nums[i] ≤ 10^9
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Follow-up
&lt;/h2&gt;

&lt;p&gt;Solve in &lt;strong&gt;O(n)&lt;/strong&gt; time without directly sorting.&lt;/p&gt;




&lt;h1&gt;
  
  
  17. Largest Number
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Medium&lt;br&gt;
&lt;strong&gt;Concepts:&lt;/strong&gt; Custom Sorting, Comparator&lt;/p&gt;


&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;Given a list of &lt;strong&gt;non-negative integers&lt;/strong&gt;, arrange them such that they form the &lt;strong&gt;largest possible number&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Return the result as a &lt;strong&gt;string&lt;/strong&gt;.&lt;/p&gt;


&lt;h2&gt;
  
  
  Example 1
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input:
nums = [10,2]

Output:
"210"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Example 2
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input:
nums = [3,30,34,5,9]

Output:
"9534330"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;Numbers must be sorted based on &lt;strong&gt;which concatenation produces a larger number&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Compare:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;34 + 3 = 343
3 + 34 = 334
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since &lt;code&gt;343 &amp;gt; 334&lt;/code&gt;, &lt;code&gt;34&lt;/code&gt; should come before &lt;code&gt;3&lt;/code&gt;.&lt;/p&gt;




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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 ≤ nums.length ≤ 100
0 ≤ nums[i] ≤ 10^9
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  18. H-Index
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Medium&lt;br&gt;
&lt;strong&gt;Concepts:&lt;/strong&gt; Sorting, Counting&lt;/p&gt;


&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;Given an array &lt;code&gt;citations&lt;/code&gt;, where &lt;code&gt;citations[i]&lt;/code&gt; is the number of citations a researcher received for their &lt;code&gt;i-th&lt;/code&gt; paper.&lt;/p&gt;

&lt;p&gt;Return the &lt;strong&gt;researcher's h-index&lt;/strong&gt;.&lt;/p&gt;


&lt;h2&gt;
  
  
  Definition
&lt;/h2&gt;

&lt;p&gt;A scientist has &lt;strong&gt;h-index = h&lt;/strong&gt; if:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;h&lt;/code&gt; papers have &lt;strong&gt;at least h citations each&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input:
citations = [3,0,6,1,5]

Output:
3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;Sorted:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[0,1,3,5,6]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are &lt;strong&gt;3 papers with at least 3 citations&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  19. Sort Array by Increasing Frequency
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Easy&lt;br&gt;
&lt;strong&gt;Concepts:&lt;/strong&gt; Sorting + HashMap&lt;/p&gt;


&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;Given an array of integers &lt;code&gt;nums&lt;/code&gt;, sort the array based on:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Increasing frequency&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;If frequency is same → &lt;strong&gt;sort by decreasing number&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;


&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input:
nums = [1,1,2,2,2,3]

Output:
[3,1,1,2,2,2]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;Frequency:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 → 2 times
2 → 3 times
3 → 1 time
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sorted by frequency:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3 → 1 time
1 → 2 times
2 → 3 times
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  20. Queue Reconstruction by Height
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Hard&lt;br&gt;
&lt;strong&gt;Concepts:&lt;/strong&gt; Sorting + Greedy&lt;/p&gt;


&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;You are given an array &lt;code&gt;people&lt;/code&gt; where:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;people[i] = [hi, ki]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;hi&lt;/code&gt; = height of person&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ki&lt;/code&gt; = number of people in front with height &lt;strong&gt;≥ hi&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Reconstruct the queue.&lt;/p&gt;




&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input:
people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]

Output:
[[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;Sort people by:&lt;/p&gt;

&lt;p&gt;1️⃣ Height &lt;strong&gt;descending&lt;/strong&gt;&lt;br&gt;
2️⃣ k-value &lt;strong&gt;ascending&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Then insert them at position &lt;code&gt;k&lt;/code&gt;.&lt;/p&gt;


&lt;h1&gt;
  
  
  21. Minimum Number of Arrows to Burst Balloons
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Medium&lt;br&gt;
&lt;strong&gt;Concepts:&lt;/strong&gt; Sorting + Greedy&lt;/p&gt;


&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;You are given balloons represented by intervals:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[xstart, xend]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An arrow shot at position &lt;code&gt;x&lt;/code&gt; bursts all balloons where:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;xstart ≤ x ≤ xend
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Return the &lt;strong&gt;minimum arrows needed&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input:
points = [[10,16],[2,8],[1,6],[7,12]]

Output:
2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;Burst:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Arrow 1 → [1,6] and [2,8]
Arrow 2 → [7,12] and [10,16]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  22. Minimum Platforms (Train Problem)
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Medium&lt;br&gt;
&lt;strong&gt;Concepts:&lt;/strong&gt; Sorting + Two Pointers&lt;/p&gt;


&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;Given arrival and departure times of trains, find the &lt;strong&gt;minimum number of platforms required&lt;/strong&gt; so that no train waits.&lt;/p&gt;


&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Arrival:
[900, 940, 950, 1100, 1500, 1800]

Departure:
[910, 1200, 1120, 1130, 1900, 2000]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;At &lt;strong&gt;11:00 AM&lt;/strong&gt;, three trains are present simultaneously.&lt;/p&gt;




&lt;h1&gt;
  
  
  23. Sort a Linked List
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Medium&lt;br&gt;
&lt;strong&gt;Concepts:&lt;/strong&gt; Merge Sort on Linked List&lt;/p&gt;


&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;Given the &lt;code&gt;head&lt;/code&gt; of a linked list, sort the list in &lt;strong&gt;ascending order&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Time complexity should be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;O(n log n)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input:
head = [4,2,1,3]

Output:
[1,2,3,4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  24. Minimum Difference Between Largest and Smallest
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Easy&lt;br&gt;
&lt;strong&gt;Concepts:&lt;/strong&gt; Sorting + Sliding Window&lt;/p&gt;


&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;Given an integer array &lt;code&gt;nums&lt;/code&gt; and integer &lt;code&gt;k&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Choose &lt;code&gt;k&lt;/code&gt; elements such that the &lt;strong&gt;difference between maximum and minimum is minimized&lt;/strong&gt;.&lt;/p&gt;


&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input:
nums = [9,4,1,7]
k = 2

Output:
2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;Sorted:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1,4,7,9]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Minimum difference:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;4 - 1 = 3
7 - 4 = 3
9 - 7 = 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Answer = &lt;strong&gt;2&lt;/strong&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Find the Distance Value Between Two Arrays Easy Solution</title>
      <dc:creator>Abhishek Gupta </dc:creator>
      <pubDate>Thu, 05 Mar 2026 09:10:54 +0000</pubDate>
      <link>https://forem.com/abhishek-nexgen-dev/find-the-distance-value-between-two-arrays-easy-solution-13c4</link>
      <guid>https://forem.com/abhishek-nexgen-dev/find-the-distance-value-between-two-arrays-easy-solution-13c4</guid>
      <description>&lt;p&gt;The problem asks us to count how many elements in &lt;code&gt;arr1&lt;/code&gt; are &lt;strong&gt;far enough&lt;/strong&gt; from every element in &lt;code&gt;arr2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For an element &lt;code&gt;x&lt;/code&gt; in &lt;code&gt;arr1&lt;/code&gt;, we must check that:&lt;/p&gt;

&lt;p&gt;[&lt;br&gt;
|x - y| &amp;gt; d&lt;br&gt;
]&lt;/p&gt;

&lt;p&gt;for &lt;strong&gt;every element &lt;code&gt;y&lt;/code&gt; in &lt;code&gt;arr2&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A simple way would be to compare every element of &lt;code&gt;arr1&lt;/code&gt; with every element of &lt;code&gt;arr2&lt;/code&gt;, but that would take &lt;strong&gt;O(n × m)&lt;/strong&gt; time.&lt;/p&gt;

&lt;p&gt;To optimize this, we can &lt;strong&gt;sort &lt;code&gt;arr2&lt;/code&gt; and use Binary Search&lt;/strong&gt;.&lt;br&gt;
Binary search helps us quickly check if there exists an element in &lt;code&gt;arr2&lt;/code&gt; whose distance from &lt;code&gt;arr1[i]&lt;/code&gt; is &lt;strong&gt;less than or equal to &lt;code&gt;d&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If such an element exists, then the element from &lt;code&gt;arr1&lt;/code&gt; &lt;strong&gt;cannot be counted&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  Approach
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Sort &lt;code&gt;arr2&lt;/code&gt;&lt;/strong&gt; so we can apply binary search.&lt;/li&gt;
&lt;li&gt;For every element &lt;code&gt;arr1[i]&lt;/code&gt;:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;binary search&lt;/strong&gt; on &lt;code&gt;arr2&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;During the search, check if&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;[&lt;br&gt;
|arr2[mid] - arr1[i]| \le d&lt;br&gt;
]&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If such an element is found, return its index immediately.&lt;/li&gt;
&lt;li&gt;If the search ends without finding such an element, return &lt;code&gt;-1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;In the main function:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;If binary search returns &lt;code&gt;-1&lt;/code&gt;, it means no element in &lt;code&gt;arr2&lt;/code&gt; is within distance &lt;code&gt;d&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Increase the count.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This reduces unnecessary comparisons and improves efficiency.&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%2Fhr45qjx7k9020cbzc3m0.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%2Fhr45qjx7k9020cbzc3m0.png" alt="Screenshot at 2026-03-05 14-34-25.png" width="800" height="578"&gt;&lt;/a&gt;&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%2Fzg3r7m8007frwo0vov2u.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%2Fzg3r7m8007frwo0vov2u.png" alt="Screenshot at 2026-03-05 14-26-48.png" width="800" height="450"&gt;&lt;/a&gt;&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%2Foihyvmywgnn2pkcbh4c6.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%2Foihyvmywgnn2pkcbh4c6.png" alt="Screenshot at 2026-03-05 14-08-09.png" width="715" height="835"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Complexity
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Time complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Sorting &lt;code&gt;arr2&lt;/code&gt; → &lt;strong&gt;O(n log n)&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Binary search for each element of &lt;code&gt;arr1&lt;/code&gt; → &lt;strong&gt;O(m log n)&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Overall:&lt;/p&gt;

&lt;p&gt;[&lt;br&gt;
O(n \log n + m \log n)&lt;br&gt;
]&lt;/p&gt;




&lt;h3&gt;
  
  
  Space complexity
&lt;/h3&gt;

&lt;p&gt;No extra data structures are used apart from variables.&lt;/p&gt;

&lt;p&gt;[&lt;br&gt;
O(1)&lt;br&gt;
]&lt;/p&gt;




&lt;h1&gt;
  
  
  Code
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * @param {number[]} arr1
 * @param {number[]} arr2
 * @param {number} d
 * @return {number}
 */&lt;/span&gt;

&lt;span class="c1"&gt;// Binary search to check if an element in arr&lt;/span&gt;
&lt;span class="c1"&gt;// is within distance &amp;lt;= d from target&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;BinarySearch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;Left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;Right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

  &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Left&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;Right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;Left&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;Right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;// If distance is within d, target is too close&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;mid&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Standard binary search movement&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;Right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;Left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// No element within distance d&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;findTheDistanceValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;arr2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="c1"&gt;// Sort arr2 for binary search&lt;/span&gt;
  &lt;span class="nx"&gt;arr2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

  &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&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="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;arr1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;

    &lt;span class="c1"&gt;// Check if arr1[i] is close to any element in arr2&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;el&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;BinarySearch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr2&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;arr1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;// If no close element found, count it&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
      &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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