<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="4.4.1">Jekyll</generator><link href="https://iamprasadraju.github.io/feed/notes.xml" rel="self" type="application/atom+xml" /><link href="https://iamprasadraju.github.io/" rel="alternate" type="text/html" /><updated>2026-07-09T07:03:57+00:00</updated><id>https://iamprasadraju.github.io/feed/notes.xml</id><title type="html">Prasad Raju G | Notes</title><subtitle>Independent AI Researcher &amp; Systems Engineer focusing on ML/DL algorithms, Python, PyTorch, and building systems engineering projects on GitHub.
</subtitle><author><name>Email me at</name><email>gls.prasadraju@gmail.com</email></author><entry><title type="html">Book - Mathematics for Machine learning</title><link href="https://iamprasadraju.github.io/notes/2026-05-02-maths-for-ml-book.html" rel="alternate" type="text/html" title="Book - Mathematics for Machine learning" /><published>2026-05-02T00:00:00+00:00</published><updated>2026-05-02T00:00:00+00:00</updated><id>https://iamprasadraju.github.io/notes/maths-for-ml-book</id><content type="html" xml:base="https://iamprasadraju.github.io/notes/2026-05-02-maths-for-ml-book.html"><![CDATA[<h3 id="what-is-the-mathematics-for-machine-learning-book-about">What is the Mathematics for Machine Learning Book About?</h3>
<p><a href="https://iamprasadraju.github.io/ResearchRack/books/mathematics-for-machine-learning-1777020629">Book url</a></p>

<pre><code class="language-mermaid">stateDiagram-v2
    direction LR
    Data --&gt; Model
    Model --&gt; Learning
</code></pre>

<p><strong>Machine learning</strong> is about designing algorithms that automatically extract
valuable information from data.</p>

<blockquote>
    <b>The goal is to find good models that generalize well to yet unseen data.</b>
</blockquote>

<h2 id="how-are-vectors-used-in-machine-learning">How Are Vectors Used in Machine Learning?</h2>

<p>Any data represented as <strong><code class="language-plaintext highlighter-rouge">vectors</code></strong>.</p>

<p>Three ways to think about <strong>vectors</strong>:</p>

<ul>
  <li>
    <p>a vector as an <strong>array of numbers</strong> (a
computer science view)</p>
  </li>
  <li>
    <p>a vector as an <strong>arrow with a direction and magnitude</strong> (a physics view)</p>
  </li>
  <li>
    <p>a vector as an object that obeys <strong>addition and scaling</strong> (a mathematical view)</p>
  </li>
</ul>

<h2 id="how-do-machine-learning-models-learn-from-data">How Do Machine Learning Models Learn From Data?</h2>

<p>Learning component of machine learning - <span style="background-color: yellow;"><strong>Training the model</strong> means to use the data available to <strong>optimize</strong> <span style="background-color: lightgreen;">(<strong>gradient descent</strong>) </span> some parameters of the model with respect to a <strong>utility function</strong> <span style="background-color: lightgreen;">(<strong>loss function</strong>)</span>  that evaluates how well the model predicts the training data.</span></p>

<p>$^{*}\text{Note:}$ <span style="background-color: #FAA0A0;"> Performing well on data that
we have already seen (<strong>training data</strong>) may only mean that we found a
good way to <strong>memorize</strong> the data. </span></p>

<h3 id="what-are-the-core-mathematical-pillars-of-machine-learning">What Are the Core Mathematical Pillars of Machine Learning?</h3>

<p><strong>Linear algebra</strong> - The study of vectors and matrices is called linear algebra. 
Numerical data as vectors and represent a table of such
data as a matrix.</p>

<p>Vectors and matrices represent datasets, features, and model parameters in ML algorithms like neural networks. Key operations include <strong>matrix multiplication</strong>, <strong>inversion</strong>, <strong>eigenvalues/eigenvectors</strong> (used in <strong>PCA</strong> for dimensionality reduction), and <strong>projections</strong> (essential for <strong>SVM</strong>).</p>

<p><strong>Calculus</strong> - Gradients and partial derivatives enable optimization techniques like <strong>gradient descent</strong> to minimize <strong>loss functions</strong>. Concepts like <strong>chain rule</strong> and <strong>Jacobian matrices</strong> support <strong>backpropagation</strong> in deep learning.</p>

<p><strong>Probability and Statistics</strong> - These handle uncertainty, data distributions, and inference. Core ideas include <strong>probability distributions</strong>, <strong>Bayes’ theorem</strong>, <strong>maximum likelihood estimation (MLE)</strong>, <strong>hypothesis testing</strong>, <strong>bias-variance tradeoff</strong>, and metrics like <strong>mean squared error</strong>.</p>

<p><strong>Optimization</strong> - Techniques such as gradient descent variants (e.g., <strong>stochastic</strong>, <strong>Adam</strong>) rely on calculus to iteratively update model weights. <strong>Convex optimization</strong> ensures reliable convergence in many ML problems.</p>]]></content><author><name>Email me at</name><email>gls.prasadraju@gmail.com</email></author><category term="Machine Learning" /><category term="Mathematics" /><category term="Data Science" /><category term="Deep Learning" /><category term="Artificial Intelligence" /><summary type="html"><![CDATA[Explore the core mathematical concepts behind Machine Learning, covering linear algebra, calculus, probability, and optimization techniques for AI models.]]></summary></entry><entry><title type="html">Stats 110</title><link href="https://iamprasadraju.github.io/notes/2026-04-17-Stats-110.html" rel="alternate" type="text/html" title="Stats 110" /><published>2026-04-17T00:00:00+00:00</published><updated>2026-04-17T00:00:00+00:00</updated><id>https://iamprasadraju.github.io/notes/Stats-110</id><content type="html" xml:base="https://iamprasadraju.github.io/notes/2026-04-17-Stats-110.html"><![CDATA[<h2 id="what-is-statistics-and-how-does-it-differ-from-mathematics">What is Statistics and How does it Differ from Mathematics?</h2>
<p>My Notes for <a href="https://stat110.hsites.harvard.edu/">stats110</a> course</p>

<blockquote>
  <p><strong>Mathematics</strong> is logic of <a href="https://en.wikipedia.org/wiki/Certainty"><strong>certainty</strong></a></p>
  <strong>Statistics</strong> is the logic of <a href="https://en.wikipedia.org/wiki/Uncertainty"><strong>uncertainty</strong></a>
</blockquote>]]></content><author><name>Email me at</name><email>gls.prasadraju@gmail.com</email></author><category term="Statistics" /><category term="Probability" /><category term="Mathematics" /><category term="Stats 110" /><summary type="html"><![CDATA[Comprehensive notes for the Harvard Stats 110 course, exploring the logic of uncertainty and probability. Discover the fundamental differences between mathematics and statistics.]]></summary></entry><entry><title type="html">Git Guide</title><link href="https://iamprasadraju.github.io/notes/2026-04-16-Git-Guide.html" rel="alternate" type="text/html" title="Git Guide" /><published>2026-04-16T00:00:00+00:00</published><updated>2026-04-16T00:00:00+00:00</updated><id>https://iamprasadraju.github.io/notes/Git-Guide</id><content type="html" xml:base="https://iamprasadraju.github.io/notes/2026-04-16-Git-Guide.html"><![CDATA[<ul>
  <li><a href="#what-is-the-best-git-branch-naming-convention">What is the Best Git Branch Naming Convention?</a></li>
  <li><a href="#how-do-you-write-an-effective-git-commit-message">How Do You Write an Effective Git Commit Message?</a></li>
</ul>

<h2 id="what-is-the-best-git-branch-naming-convention">What is the Best Git Branch Naming Convention?</h2>
<hr />
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre>git branch &lt;category/reference/description-in-kebab-case&gt;
</pre></td></tr></tbody></table></code></pre></div></div>

<p>A git branch should start with a category. Pick one of these: <strong>feature</strong>, <strong>bugfix</strong>, <strong>hotfix</strong>, or <strong>test</strong>.</p>

<ul>
  <li><strong><code class="language-plaintext highlighter-rouge">feature</code></strong> is for adding, refactoring or removing a feature</li>
  <li><strong><code class="language-plaintext highlighter-rouge">bugfix</code></strong> is for fixing a bug</li>
  <li><strong><code class="language-plaintext highlighter-rouge">hotfix</code></strong> is for changing code with a temporary solution and/or without following the usual process (usually because of an emergency).</li>
  <li><strong><code class="language-plaintext highlighter-rouge">test</code></strong> is for experimenting outside of an issue/ticket</li>
  <li><strong><code class="language-plaintext highlighter-rouge">doc</code></strong> is for adding, changing or removing documentation</li>
</ul>

<h4 id="how-to-include-issue-references">How to Include Issue References?</h4>

<p>After the category, there should be a “<code class="language-plaintext highlighter-rouge">/</code>” followed by the reference of the <strong>issue/ticket/task</strong> you are working on. If there’s no reference, just add <strong>no-ref</strong>. With task it is meant as benchmarking task e.g. <strong>batch_integration</strong>.</p>

<h4 id="how-to-write-a-branch-description">How to Write a Branch Description?</h4>

<p>After the reference, there should be another “<code class="language-plaintext highlighter-rouge">/</code>” followed by a <strong>description</strong> which sums up the purpose of this specific branch. This description should be short and “<code class="language-plaintext highlighter-rouge">kebab-cased</code>”.</p>

<p>By default, you can use the title of the issue/ticket you are working on. Just replace any special character by “<code class="language-plaintext highlighter-rouge">-</code>”.</p>

<h4 id="what-are-some-examples-of-git-branch-names">What are Some Examples of Git Branch Names?</h4>

<ul>
  <li>You need to add, refactor or remove a feature: <code class="language-plaintext highlighter-rouge">git branch feature/issue-42/create-new-button-component</code></li>
  <li>You need to fix a bug: <code class="language-plaintext highlighter-rouge">git branch bugfix/issue-342/button-overlap-form-on-mobile</code></li>
  <li>You need to fix a bug really fast (possibly with a temporary solution): <code class="language-plaintext highlighter-rouge">git branch hotfix/no-ref/registration-form-not-working</code></li>
  <li>You need to experiment outside of an issue/ticket: <code class="language-plaintext highlighter-rouge">git branch test/no-ref/refactor-components-with-atomic-design</code></li>
</ul>

<p>*<a href="https://openproblems.bio/documentation/reference/naming/#references">Reference</a></p>

<h2 id="how-do-you-write-an-effective-git-commit-message">How Do You Write an Effective Git Commit Message?</h2>
<hr />

<table>
  <thead>
    <tr>
      <th>Type</th>
      <th>Use Case</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">feat</code></td>
      <td>Adding a new <strong>feature</strong> for the user.</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">fix</code></td>
      <td>Fixing a <strong>bug</strong>.</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">docs</code></td>
      <td><strong>Documentation-only</strong> changes.</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">style</code></td>
      <td><strong>Formatting</strong>, missing semi-colons; no code logic change.</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">refactor</code></td>
      <td>Code changes that neither fix a bug nor add a feature.</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">perf</code></td>
      <td>Changes that improve <strong>performance</strong>.</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">test</code></td>
      <td>Adding or correcting <strong>tests</strong>.</td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">chore</code></td>
      <td>Routine tasks like updating <strong>dependencies</strong> or <strong>build tools</strong>.</td>
    </tr>
  </tbody>
</table>

<h3 id="why-are-git-commit-messages-so-important">Why Are Git Commit Messages So Important?</h3>

<ul>
  <li>
    <p>When you are applying for jobs, employers will look through your projects on GitHub and they will look through your <strong>commit history</strong>. Having good commits as a novice developer will help you stand out.</p>
  </li>
  <li>
    <p>Having a good commit message history will allow you (or other developers working on your code) to quickly see <strong>what changes were made and why</strong>. This is useful if a bug is found in the code that needs to be fixed!</p>
  </li>
  <li>
    <p>Having a good commit message history will also be helpful if you come back to a project you were working on after stepping away from it for a while. You likely won’t remember your <strong>thought process</strong> and changes made when initially writing out the code.</p>
  </li>
</ul>

<h3 id="what-is-the-difference-between-bad-and-good-commits">What is the Difference Between Bad and Good Commits?</h3>

<p>When it comes to writing commits, it is crucial to know how to write them effectively. Here’s an example of a <strong>bad commit message</strong>:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre>fix a bug
</pre></td></tr></tbody></table></code></pre></div></div>

<p>Even though it describes what you did, the message is <strong>too vague</strong>, which leaves the other developers on your team confused. A <strong>good commit message</strong> will explain the <strong>why</strong> behind your changes. In other words, a commit message describes <strong>what problem your changes solve and how it solves them</strong>.</p>

<p>Effective commits consist of two separate parts: a <strong>subject</strong>, and a <strong>body</strong>:</p>

<p><strong>Subject</strong></p>

<p>A brief summary of the change you made to the project.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre>This is the change I made to the codebase
</pre></td></tr></tbody></table></code></pre></div></div>
<blockquote>Note: Commit subject character limit
  GitHub has a <strong>72-character limit</strong>, so we recommend keeping your commits’ subjects within this amount.</blockquote>

<h3 id="what-should-be-in-the-commit-message-body">What Should Be in the Commit Message Body?</h3>

<p>A concise yet clear description of what you did.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre>Describe the problem your commit solves and how.
</pre></td></tr></tbody></table></code></pre></div></div>
<p>Now that we learned the secret to creating a good commit message, let’s try and fix the commit message from earlier:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
</pre></td><td class="rouge-code"><pre>Add missing link and alt text to the company's logo

Screen readers won't read the images to users with disabilities without this information.
</pre></td></tr></tbody></table></code></pre></div></div>
<p>Ahh, that’s better! :) Now, developers can gain a better understanding of this commit message because it does the following:</p>

<ul>
  <li>Provides a <strong>subject</strong> that specifies your code’s action (e.g., “Add missing link and alt text to the company’s logo”).</li>
  <li>Contains a <strong>body</strong> that provides a concise yet clear description of why the commit needed to be made (e.g., “Screen readers won’t read the images to users with disabilities without this information.”).</li>
  <li><strong>Separates the subject from the body</strong> with a new/blank line. This is a best practice we highly recommend following. It makes commit messages easier for other developers to read.</li>
</ul>

<p>Some teams of developers might have their own convention for commit messages in specific formats. However, they still follow the same principles as this lesson outlined.</p>

<h3 id="how-do-you-commit-with-a-subject-and-body">How Do You Commit with a Subject and Body?</h3>

<p>Up until now, you’ve been told to commit with <code class="language-plaintext highlighter-rouge">git commit -m &lt;message&gt;</code>. To make a commit with subject and body in message, the simplest way is to type <code class="language-plaintext highlighter-rouge">git commit</code> without the <code class="language-plaintext highlighter-rouge">-m</code> flag and message argument.</p>

<h3 id="when-should-you-make-a-git-commit">When Should You Make a Git Commit?</h3>
<p>A good way to view a commit is like a <strong>“snapshot”</strong> of your code at the moment that it was made. That version of your code up to that point will be saved for you to revert back to or look back at.</p>

<p>When writing code, it’s considered best practice to <strong>commit every time you have a meaningful change</strong> in the code. This will create a timeline of your progress and show that your finished code didn’t appear out of nowhere.</p>

<p>In other words, make a commit if you get a piece of code you are working on to <strong>function like you want it to</strong>, <strong>fix a typo</strong>, or <strong>fix a bug</strong>. As you gain experience, you will develop a better feel for what should be committed!</p>

<p>There will come a time when you are working on a project and you FINALLY get something just right (this would be a good time to commit), and then maybe 30 seconds to a few days later it breaks. You have no idea what you changed, everything looks to be the same and you don’t remember editing that line, but alas, it isn’t working how you want it anymore. You’d be able to go back through your commit history and either <strong>revert your code back to the last commit</strong> you made when you first got that part working or go back and see what your code looked like at that point in time.</p>

<p>*<a href="https://www.theodinproject.com/lessons/foundations-commit-messages#body">Reference</a></p>]]></content><author><name>Email me at</name><email>gls.prasadraju@gmail.com</email></author><category term="Git" /><category term="Version Control" /><category term="Best Practices" /><category term="Developer Guide" /><summary type="html"><![CDATA[A comprehensive guide on Git best practices, including branch naming conventions and how to write clear, effective commit messages.]]></summary></entry><entry><title type="html">Learning Stats</title><link href="https://iamprasadraju.github.io/notes/2026-04-14-Learning-Stats.html" rel="alternate" type="text/html" title="Learning Stats" /><published>2026-04-14T00:00:00+00:00</published><updated>2026-04-14T00:00:00+00:00</updated><id>https://iamprasadraju.github.io/notes/Learning-Stats</id><content type="html" xml:base="https://iamprasadraju.github.io/notes/2026-04-14-Learning-Stats.html"><![CDATA[<p>A collection of courses and resources that I find interesting while learning statistics.</p>

<h2 id="what-are-the-best-courses-for-learning-statistics">What are the best courses for learning statistics?</h2>
<hr />

<h3 id="1-statistics-110-probability---youtube-link">1. Statistics 110: Probability - <a href="https://www.youtube.com/playlist?list=PL2SOU6wwxB0uwwH80KTQ6ht66KWxbzTIo">Youtube Link</a></h3>

<p>Taught by <a href="https://www.edx.org/bio/joseph-blitzstein">Joseph Blitzstein</a></p>

<p>Stat 110 is an introduction to <strong>probability</strong> as a language and set of tools for understanding <strong>statistics</strong>, <strong>science</strong>, <strong>risk</strong>, and <strong>randomness</strong>. The ideas and methods are useful in statistics, science, engineering, economics, finance, and everyday life. Topics include the following. Basics: <strong>sample spaces and events</strong>, conditioning, <strong>Bayes’ Theorem</strong>. <strong>Random variables</strong> and their distributions: distributions, <strong>moment generating functions</strong>, expectation, variance, covariance, correlation, conditional expectation. Univariate distributions: <strong>Normal, t, Binomial, Negative Binomial, Poisson, Beta, Gamma</strong>. Multivariate distributions: joint, conditional, and marginal distributions, independence, transformations, Multinomial, Multivariate Normal. Limit theorems: <strong>law of large numbers</strong>, <strong>central limit theorem</strong>. <strong>Markov chains</strong>: transition probabilities, stationary distributions, reversibility, convergence. The prerequisites are calculus (mainly single variable) and familiarity with matrices.</p>

<h3 id="2-mit-18650-statistics-for-applications-fall-2016----youtube-link">2. MIT 18.650 Statistics for Applications, Fall 2016  - <a href="https://www.youtube.com/playlist?list=PLUl4u3cNGP60uVBMaoNERc6knT_MgPKS0">Youtube Link</a></h3>

<p>Taught by <a href="https://math.mit.edu/directory/profile.html?pid=1654">Philippe Rigollet</a></p>

<p>This course offers an in-depth exploration of the <strong>theoretical foundations for statistical methods</strong> that are useful in many applications. The goal is to understand the role of mathematics in the research and development of <strong>efficient statistical methods</strong>.</p>

<h2 id="which-books-are-recommended-for-statistical-learning">Which books are recommended for statistical learning?</h2>
<hr />

<h3 id="1-an-introduction-to-statistical-learning-with-applications-in-python">1. An Introduction To Statistical Learning With Applications In Python</h3>
<p><a href="https://iamprasadraju.github.io/ResearchRack/_pdfs/1773664768_ISLP_website.pdf">Read Book</a></p>

<p><img width="20%" src="https://m.media-amazon.com/images/I/51KWK0SXEXL._SY522_.jpg" /></p>]]></content><author><name>Email me at</name><email>gls.prasadraju@gmail.com</email></author><category term="Statistics" /><category term="Probability" /><category term="Python" /><category term="Data Science" /><category term="Machine Learning" /><summary type="html"><![CDATA[A curated collection of top resources, including Harvard and MIT courses, and foundational textbooks for mastering statistics and probability in Python.]]></summary></entry><entry><title type="html">Mac Setup</title><link href="https://iamprasadraju.github.io/notes/2026-04-11-Mac-Setup.html" rel="alternate" type="text/html" title="Mac Setup" /><published>2026-04-11T00:00:00+00:00</published><updated>2026-04-11T00:00:00+00:00</updated><id>https://iamprasadraju.github.io/notes/Mac-Setup</id><content type="html" xml:base="https://iamprasadraju.github.io/notes/2026-04-11-Mac-Setup.html"><![CDATA[<p>Step-by-step instructions to setup new Mac</p>

<h3 id="which-cli-tools-are-essential-for-a-new-mac">Which CLI tools are essential for a new Mac?</h3>
<hr />

<ol>
  <li>
    <p><a href="https://mac.install.guide/commandlinetools/">Xcode Command Line Tools</a> <code class="language-plaintext highlighter-rouge">
 xcode-select –-install
 </code> it comes with <code class="language-plaintext highlighter-rouge">clang</code>, <code class="language-plaintext highlighter-rouge">gcc</code>, <code class="language-plaintext highlighter-rouge">clang++</code>, <code class="language-plaintext highlighter-rouge">g++</code>, <code class="language-plaintext highlighter-rouge">git</code> and more …</p>

    <p><strong>List of Xcode Command Line Tools</strong></p>

    <div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre>   <span class="nb">ls</span> /Library/Developer/CommandLineTools/usr/bin/
</pre></td></tr></tbody></table></code></pre></div>    </div>
  </li>
  <li>
    <p><a href="https://en.wikipedia.org/wiki/Homebrew_(package_manager)">Homebrew</a> is a <strong>free and open-source software package management system</strong> that simplifies the installation of software on Apple’s operating system, macOS, as well as Linux. <a href="https://brew.sh/">official Homebrew site</a></p>
  </li>
  <li>
    <p><a href="https://git-scm.com/">Git</a> Already comes with Xcode CLT</p>
  </li>
  <li>
    <p><a href="https://github.com/iamprasadraju/gen-cli">Gen CLI</a> (My own CLI tool)<code class="language-plaintext highlighter-rouge">pip install gen-cli</code></p>
  </li>
  <li>
    <p><a href="http://opencode.ai/">opencode</a> is an <strong>open source agent</strong> that helps you write code in your terminal, IDE, or desktop.</p>
  </li>
  <li>
    <p><a href="https://tmuxcheatsheet.com">tmux</a> (short for Terminal Multiplexer) is a command-line tool that lets you manage multiple terminal sessions within a single window. <code class="language-plaintext highlighter-rouge">brew install tmux</code></p>
  </li>
</ol>

<h3 id="what-software-and-applications-should-i-install">What software and applications should I install?</h3>
<hr />

<ol>
  <li><a href="https://zen-browser.app">Zen Browser</a> is a free, open-source web browser based on Mozilla Firefox. It combines Firefox’s privacy and plugin ecosystem with the sleek, modern, vertical-tab interface popularized by the Arc browser.</li>
  <li><a href="https://obsproject.com/">OBS</a> <strong>Free and open source software</strong> for video recording and live streaming.</li>
  <li><a href="https://zed.dev/">Zed</a> is a <strong>Rust based code editor</strong>.</li>
  <li><a href="https://windsurf.com/">Windsurf</a> is a <strong>free and open-source code editor</strong>. (for unlimited inline autocomplete)</li>
  <li><a href="https://discord.com/">Discord</a> is a <strong>free voice and text communication application</strong> and digital distribution platform.</li>
  <li><a href="https://open.spotify.com/download">Spotify</a> is a <strong>popular digital audio streaming service</strong> for music listening.</li>
  <li><a href="https://obsidian.md/">Obsidian</a> <strong>stores notes privately</strong> on your device as markdown files.</li>
  <li><a href="https://github.com/mhaeuser/Battery-Toolkit">Battery Toolkit</a> <strong>Control the platform power state</strong> of your Apple Silicon Mac. Battery Toolkit allows specifying a hard limit past which battery charging will be turned off.</li>
  <li><a href="https://apps.apple.com/us/app/macdroid-manager-for-android/id1476545828?mt=12">MacDroid</a> the <strong>ultimate solution for transferring files</strong> between Android and Mac.</li>
  <li><a href="https://rectangleapp.com/">Rectangle</a> <strong>Move and resize windows</strong> in macOS using keyboard shortcuts or snap areas.</li>
  <li><a href="http://mos.caldis.me/">Mos</a> is a <strong>free, open-source macOS utility</strong> that makes wheel scrolling feel closer to a trackpad, without getting in your way.</li>
  <li><a href="https://betterdisplay.pro/">BetterDisplay</a> is a <strong>macOS utility that allows you to customize your display settings</strong>. (external display)</li>
  <li><a href="https://mac-stats.com">Stas</a> is a free, open-source macOS system monitor that lives in your menu bar - track CPU, GPU, RAM, disk, network, sensors, battery and more on Apple Silicon and Intel Macs.</li>
  <li><a href="https://github.com/nghialuong/Lidless">Lidless</a> tiny macOS menu-bar app that keeps your Mac running even with the lid closed — so coding agents (Claude Code, Codex, etc.) keep working while you move around.</li>
</ol>

<h3 id="how-do-i-install-core-programming-languages">How do I install core programming languages?</h3>
<hr />

<ol>
  <li><a href="https://www.python.org/">Python</a> <code class="language-plaintext highlighter-rouge">brew install python</code></li>
  <li><a href="https://www.ruby-lang.org/en/documentation/installation/#homebrew">Ruby</a> <code class="language-plaintext highlighter-rouge">brew install ruby</code></li>
  <li><a href="https://en.wikipedia.org/wiki/C_(programming_language)">C</a> Already comes With Xcode CLT i.e <code class="language-plaintext highlighter-rouge">gcc</code> and <code class="language-plaintext highlighter-rouge">clang</code></li>
  <li><a href="https://en.wikipedia.org/wiki/C%2B%2B">C++</a> Already comes With Xcode CLT i.e <code class="language-plaintext highlighter-rouge">g++</code> and <code class="language-plaintext highlighter-rouge">clang++</code></li>
</ol>

<p>or (3, 4)</p>

<ol>
  <li><a href="https://llvm.org/">llvm</a> <code class="language-plaintext highlighter-rouge">brew install llvm</code> clang and clang++ compiler (<strong>Recommended</strong>)</li>
</ol>

<h3 id="which-python-libraries-are-required-for-machine-learning-and-data-science">Which Python libraries are required for machine learning and data science?</h3>
<hr />

<ol>
  <li><a href="https://numpy.org/doc/stable/">Numpy</a> the <strong>fundamental package for scientific computing</strong> with Python.<code class="language-plaintext highlighter-rouge">pip install numpy</code></li>
  <li><a href="https://pandas.pydata.org/docs/">Pandas</a> is a <strong>fast, powerful, flexible and easy to use open source data analysis and manipulation tool</strong>,
built on top of the Python programming language. <code class="language-plaintext highlighter-rouge">pip install pandas</code></li>
  <li><a href="https://scikit-learn.org/stable/user_guide.html">Scikit-learn</a> is an <strong>open source machine learning library</strong> that supports supervised and unsupervised learning. It also provides various tools for model fitting, data preprocessing, model selection, model evaluation, and many other utilities. <code class="language-plaintext highlighter-rouge">pip install scikit-learn</code></li>
  <li><a href="https://seaborn.pydata.org/index.html">Seaborn</a> is a <strong>Python data visualization library</strong> based on matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics. <code class="language-plaintext highlighter-rouge">pip install seaborn</code></li>
  <li><a href="https://matplotlib.org/">Matplotlib</a> is a <strong>comprehensive library for creating static, animated, and interactive visualizations</strong> in Python. <code class="language-plaintext highlighter-rouge">pip install matplotlib</code></li>
  <li><a href="https://docs.pytorch.org/docs/stable/index.html#">Pytorch</a> is an <strong>open-source optimized tensor library</strong> for deep learning using GPUs and CPUs <strong>Deep learning library</strong> <code class="language-plaintext highlighter-rouge">pip install torch</code></li>
  <li><a href="https://www.tensorflow.org/api_docs">Tensorflow</a> is an <strong>open-source, end-to-end machine learning platform</strong> developed by Google, specializing in deep neural networks, numerical computation, and large-scale machine learning. <code class="language-plaintext highlighter-rouge">pip install tensorflow</code></li>
  <li><a href="https://docs.scipy.org/doc/scipy/">Scipy</a> is a <strong>collection of mathematical algorithms and convenience functions</strong> built on NumPy . It adds significant power to Python. <code class="language-plaintext highlighter-rouge">pip install scipy</code></li>
  <li><a href="http://jupyter.org/">Jupyter Notebook</a> is the latest <strong>web-based interactive development environment</strong> for notebooks, code, and data. Its flexible interface allows users to configure and arrange workflows in data science, scientific computing, computational journalism, and machine learning. A modular design invites extensions to expand and enrich functionality. <code class="language-plaintext highlighter-rouge">pip install notebook</code></li>
  <li><a href="https://docs.opencv.org/4.x/">OpenCV</a> is a <strong>library of programming functions</strong> mainly for real-time <a href="https://en.wikipedia.org/wiki/Computer_vision">computer vision</a>  <code class="language-plaintext highlighter-rouge">pip install opencv-python</code></li>
</ol>

<p><a href="https://github.com/iamprasadraju/configuration/blob/main/requirements.txt">requirements.txt</a></p>]]></content><author><name>Email me at</name><email>gls.prasadraju@gmail.com</email></author><category term="Mac" /><category term="Setup" /><category term="Development" /><category term="Python" /><category term="CLI Tools" /><summary type="html"><![CDATA[A comprehensive guide to setting up a new Mac for development. Includes step-by-step instructions for installing CLI tools, essential software, programming languages, and Python libraries.]]></summary></entry><entry><title type="html">In-built Datastructures in Python, C++, C</title><link href="https://iamprasadraju.github.io/notes/2026-03-08-in-built-datastructures.html" rel="alternate" type="text/html" title="In-built Datastructures in Python, C++, C" /><published>2026-03-08T00:00:00+00:00</published><updated>2026-03-08T00:00:00+00:00</updated><id>https://iamprasadraju.github.io/notes/in-built-datastructures</id><content type="html" xml:base="https://iamprasadraju.github.io/notes/2026-03-08-in-built-datastructures.html"><![CDATA[<p>A quick reference for common data structure operations. Use this as your go-to guide.</p>

<hr />

<h2 id="what-is-in-this-guide">What is in this Guide?</h2>

<h3 id="python">Python</h3>
<p><a href="#list">List</a> | <a href="#tuple">Tuple</a> | <a href="#set">Set</a> | <a href="#dictionary">Dictionary</a> | <a href="#array-module">Array</a> | <a href="#collections">Collections</a></p>

<h3 id="c-stl">C++ (STL)</h3>
<p><a href="#vector">Vector</a> | <a href="#deque">Deque</a> | <a href="#list-cpp">List</a> | <a href="#array-cpp">Array</a> | <a href="#stack">Stack</a> | <a href="#queue">Queue</a> | <a href="#priority-queue">Priority Queue</a> | <a href="#set-cpp">Set</a> | <a href="#multiset">Multiset</a> | <a href="#map">Map</a> | <a href="#multimap">Multimap</a> | <a href="#unordered-set">Unordered Set</a> | <a href="#unordered-map">Unordered Map</a> | <a href="#string-cpp">String</a> | <a href="#pair-tuple">Pair/Tuple</a></p>

<h3 id="c-language">C Language</h3>
<p><a href="#array-c">Array</a> | <a href="#string-c">String</a> | <a href="#struct">Struct</a> | <a href="#union">Union</a> | <a href="#enum">Enum</a> | <a href="#pointer">Pointer</a> | <a href="#linked-list">Linked List</a></p>

<hr />

<h2 id="what-are-the-in-built-data-structures-in-python"><a name="python"></a>What are the In-Built Data Structures in Python?</h2>

<h3 id="how-do-you-use-a-list--in-python"><a name="list"></a>How do you use a List [] in Python?</h3>
<p><strong>Definition:</strong> <strong>Ordered</strong>, <strong>mutable sequence</strong> that allows duplicate elements. Elements are indexed starting from 0. Supports <strong>dynamic resizing</strong>.</p>

<table>
  <thead>
    <tr>
      <th>Operation</th>
      <th>Time</th>
      <th>Space</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Access by index</td>
      <td>O(1)</td>
      <td>-</td>
    </tr>
    <tr>
      <td>Append</td>
      <td>O(1)*</td>
      <td>-</td>
    </tr>
    <tr>
      <td>Insert at index</td>
      <td>O(n)</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>Remove at index</td>
      <td>O(n)</td>
      <td>-</td>
    </tr>
    <tr>
      <td>Remove by value</td>
      <td>O(n)</td>
      <td>-</td>
    </tr>
    <tr>
      <td>Search</td>
      <td>O(n)</td>
      <td>-</td>
    </tr>
    <tr>
      <td>Sort</td>
      <td>O(n log n)</td>
      <td>O(n)</td>
    </tr>
  </tbody>
</table>

<p>*Amortized</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
</pre></td><td class="rouge-code"><pre><span class="c1"># Creation
</span><span class="n">lst</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
<span class="n">lst</span> <span class="o">=</span> <span class="nf">list</span><span class="p">(</span><span class="nf">range</span><span class="p">(</span><span class="mi">5</span><span class="p">))</span>
<span class="n">lst</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="mi">5</span>           <span class="c1"># [0, 0, 0, 0, 0]
</span>
<span class="c1"># Add
</span><span class="n">lst</span><span class="p">.</span><span class="nf">append</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>           <span class="c1"># O(1) - add to end
</span><span class="n">lst</span><span class="p">.</span><span class="nf">insert</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>        <span class="c1"># O(n) - insert at index
</span><span class="n">lst</span><span class="p">.</span><span class="nf">extend</span><span class="p">(</span><span class="n">iterable</span><span class="p">)</span>    <span class="c1"># O(k) - add multiple
</span>
<span class="c1"># Remove
</span><span class="n">lst</span><span class="p">.</span><span class="nf">pop</span><span class="p">()</span>              <span class="c1"># O(1) - remove last
</span><span class="n">lst</span><span class="p">.</span><span class="nf">pop</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>              <span class="c1"># O(n) - remove at index
</span><span class="n">lst</span><span class="p">.</span><span class="nf">remove</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>           <span class="c1"># O(n) - remove first occurrence
</span><span class="n">lst</span><span class="p">.</span><span class="nf">clear</span><span class="p">()</span>             <span class="c1"># O(n)
</span>
<span class="c1"># Access
</span><span class="n">lst</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>                 <span class="c1"># O(1)
</span><span class="n">lst</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>                <span class="c1"># last element
</span>
<span class="c1"># Search
</span><span class="n">lst</span><span class="p">.</span><span class="nf">index</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>           <span class="c1"># O(n) - first index
</span><span class="n">lst</span><span class="p">.</span><span class="nf">count</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>           <span class="c1"># O(n) - count
</span><span class="n">x</span> <span class="ow">in</span> <span class="n">lst</span>               <span class="c1"># O(n) - membership
</span>
<span class="c1"># Sort/Reverse
</span><span class="n">lst</span><span class="p">.</span><span class="nf">sort</span><span class="p">()</span>             <span class="c1"># O(n log n)
</span><span class="nf">sorted</span><span class="p">(</span><span class="n">lst</span><span class="p">)</span>            <span class="c1"># O(n log n)
</span><span class="n">lst</span><span class="p">.</span><span class="nf">reverse</span><span class="p">()</span>          <span class="c1"># O(n)
</span><span class="n">lst</span><span class="p">[::</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>              <span class="c1"># O(n) - reversed copy
</span>
<span class="c1"># Stack using list
</span><span class="n">stack</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">stack</span><span class="p">.</span><span class="nf">append</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>    <span class="c1"># push - O(1)
</span><span class="n">stack</span><span class="p">.</span><span class="nf">pop</span><span class="p">()</span>        <span class="c1"># pop - O(1)
</span><span class="n">stack</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>          <span class="c1"># peek - O(1)
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p><strong>Interview Questions:</strong></p>

<p><strong>Q: When would you use list instead of array?</strong><br />
A: Use list when you need dynamic sizing, mixed types, or built-in methods. Use array module when you need memory efficiency for large numeric data of the same type.</p>

<p><strong>Q: What is the time complexity of <code class="language-plaintext highlighter-rouge">append()</code> vs <code class="language-plaintext highlighter-rouge">insert()</code>?</strong><br />
A: append() is O(1) amortized - adds to end. insert() is O(n) - requires shifting all elements.</p>

<p><strong>Q: How does list handle dynamic resizing?</strong><br />
A: Python list uses over-allocation. When full, allocates ~12.5% more space. This amortizes resize cost to O(1) for append.</p>

<p><strong>Q: Difference between <code class="language-plaintext highlighter-rouge">remove()</code> and <code class="language-plaintext highlighter-rouge">pop()</code>?</strong><br />
A: remove() removes first occurrence of value (O(n)), raises ValueError if not found. pop() removes and returns element at index.</p>

<p><strong>Q: How would you implement a stack using list?</strong><br />
A: Use append() for push, pop() for pop. Top of stack is last element.</p>

<hr />

<h3 id="when-should-you-use-a-tuple-"><a name="tuple"></a>When should you use a Tuple ()?</h3>
<p><strong>Definition:</strong> <strong>Ordered</strong>, <strong>immutable sequence</strong>. Once created, cannot be modified. Used for <strong>fixed collections</strong>.</p>

<table>
  <thead>
    <tr>
      <th>Operation</th>
      <th>Time</th>
      <th>Space</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Access by index</td>
      <td>O(1)</td>
      <td>-</td>
    </tr>
    <tr>
      <td>Search</td>
      <td>O(n)</td>
      <td>-</td>
    </tr>
    <tr>
      <td>Length</td>
      <td>O(1)</td>
      <td>-</td>
    </tr>
  </tbody>
</table>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="rouge-code"><pre><span class="c1"># Creation
</span><span class="n">t</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="n">t</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span>           <span class="c1"># parentheses optional
</span><span class="n">t</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span><span class="p">,)</span>              <span class="c1"># single element needs comma
</span><span class="n">t</span> <span class="o">=</span> <span class="nf">tuple</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>

<span class="c1"># Access - same as list
</span><span class="n">t</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">t</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="n">t</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">3</span><span class="p">]</span>

<span class="c1"># Unpack
</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span> <span class="o">=</span> <span class="n">t</span>
<span class="n">first</span><span class="p">,</span> <span class="o">*</span><span class="n">rest</span> <span class="o">=</span> <span class="n">t</span>      <span class="c1"># rest is [2, 3]
</span>
<span class="c1"># Operations
</span><span class="nf">len</span><span class="p">(</span><span class="n">t</span><span class="p">),</span> <span class="n">t</span><span class="p">.</span><span class="nf">index</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">t</span><span class="p">.</span><span class="nf">count</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>

<span class="c1"># Tuple as dict key
</span><span class="n">d</span> <span class="o">=</span> <span class="p">{(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">):</span> <span class="sh">"</span><span class="s">point</span><span class="sh">"</span><span class="p">}</span>

<span class="c1"># Multiple return values
</span><span class="k">def</span> <span class="nf">func</span><span class="p">():</span>
    <span class="k">return</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span>
<span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="nf">func</span><span class="p">()</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p><strong>Interview Questions:</strong></p>

<p><strong>Q: Why is tuple immutable? What are the benefits?</strong><br />
A: Immutability provides hashability (can be dict keys), memory efficiency, and thread safety.</p>

<p><strong>Q: Can tuple contain mutable objects? What happens?</strong><br />
A: Yes, tuple can contain mutable objects like lists. The tuple itself can’t change, but contents can.</p>

<p><strong>Q: Difference between tuple and list?</strong><br />
A: Tuple is immutable (faster, less memory, hashable), used for fixed data. List is mutable.</p>

<p><strong>Q: How does tuple hash work?</strong><br />
A: Only works if all elements are hashable. Hash is computed from contents.</p>

<p><strong>Q: Use cases where tuple is preferred over list?</strong><br />
A: Function return values, dictionary keys, fixed records (coordinates).</p>

<hr />

<h3 id="how-does-a-set--work"><a name="set"></a>How does a Set {} work?</h3>
<p><strong>Definition:</strong> <strong>Unordered collection</strong> of <strong>unique elements</strong>. No duplicates. Provides <strong>O(1) average-case lookup</strong>.</p>

<table>
  <thead>
    <tr>
      <th>Operation</th>
      <th>Time (Avg)</th>
      <th>Time (Worst)</th>
      <th>Space</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Add</td>
      <td>O(1)</td>
      <td>O(n)</td>
      <td>-</td>
    </tr>
    <tr>
      <td>Remove</td>
      <td>O(1)</td>
      <td>O(n)</td>
      <td>-</td>
    </tr>
    <tr>
      <td>Search/Membership</td>
      <td>O(1)</td>
      <td>O(n)</td>
      <td>-</td>
    </tr>
    <tr>
      <td>Union</td>
      <td>O(n+m)</td>
      <td>-</td>
      <td>O(n+m)</td>
    </tr>
    <tr>
      <td>Intersection</td>
      <td>O(min(n,m))</td>
      <td>O(n*m)</td>
      <td>-</td>
    </tr>
  </tbody>
</table>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="rouge-code"><pre><span class="c1"># Creation
</span><span class="n">s</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">}</span>
<span class="n">s</span> <span class="o">=</span> <span class="nf">set</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>
<span class="n">s</span> <span class="o">=</span> <span class="nf">set</span><span class="p">()</span>             <span class="c1"># empty set (not {})
</span>
<span class="c1"># Add/Remove
</span><span class="n">s</span><span class="p">.</span><span class="nf">add</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>             <span class="c1"># O(1)
</span><span class="n">s</span><span class="p">.</span><span class="nf">remove</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>           <span class="c1"># O(1) - raises KeyError if not found
</span><span class="n">s</span><span class="p">.</span><span class="nf">discard</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>          <span class="c1"># O(1) - no error if not found
</span><span class="n">s</span><span class="p">.</span><span class="nf">pop</span><span class="p">()</span>               <span class="c1"># O(1) - remove random element
</span>
<span class="c1"># Set operations
</span><span class="n">A</span> <span class="o">|</span> <span class="n">B</span>                 <span class="c1"># union - O(n+m)
</span><span class="n">A</span> <span class="o">&amp;</span> <span class="n">B</span>                 <span class="c1"># intersection - O(min(n,m))
</span><span class="n">A</span> <span class="o">-</span> <span class="n">B</span>                 <span class="c1"># difference - O(n)
</span><span class="n">A</span> <span class="o">^</span> <span class="n">B</span>                 <span class="c1"># symmetric difference - O(n+m)
</span>
<span class="c1"># Common elements
</span><span class="n">list1</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">]</span>
<span class="n">list2</span> <span class="o">=</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">]</span>
<span class="n">common</span> <span class="o">=</span> <span class="nf">list</span><span class="p">(</span><span class="nf">set</span><span class="p">(</span><span class="n">list1</span><span class="p">)</span> <span class="o">&amp;</span> <span class="nf">set</span><span class="p">(</span><span class="n">list2</span><span class="p">))</span>

<span class="c1"># Remove duplicates, preserve order
</span><span class="n">lst</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">]</span>
<span class="n">unique</span> <span class="o">=</span> <span class="nf">list</span><span class="p">(</span><span class="nb">dict</span><span class="p">.</span><span class="nf">fromkeys</span><span class="p">(</span><span class="n">lst</span><span class="p">))</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p><strong>Interview Questions:</strong></p>

<p><strong>Q: How is set implemented internally in Python?</strong><br />
A: Python set uses a hash table. Elements are hashed, collisions handled using open addressing.</p>

<p><strong>Q: What is the difference between set and frozenset?</strong><br />
A: set is mutable, frozenset is immutable and hashable.</p>

<p><strong>Q: How to find common elements between two lists?</strong><br />
A: Convert both to sets and use intersection.</p>

<p><strong>Q: Why set doesn’t support indexing?</strong><br />
A: Sets are hash tables - order is determined by hash values.</p>

<p><strong>Q: How would you remove duplicates from a list while preserving order?</strong><br />
A: Use dict.fromkeys() or loop through with seen set.</p>

<hr />

<h3 id="how-to-manage-key-value-pairs-with-a-dictionary-"><a name="dictionary"></a>How to manage key-value pairs with a Dictionary {}?</h3>
<p><strong>Definition:</strong> Collection of <strong>key-value pairs</strong> where each key is unique. Provides <strong>O(1) average-case lookup</strong>.</p>

<table>
  <thead>
    <tr>
      <th>Operation</th>
      <th>Time (Avg)</th>
      <th>Time (Worst)</th>
      <th>Space</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Access by key</td>
      <td>O(1)</td>
      <td>O(n)</td>
      <td>-</td>
    </tr>
    <tr>
      <td>Insert</td>
      <td>O(1)</td>
      <td>O(n)</td>
      <td>-</td>
    </tr>
    <tr>
      <td>Delete</td>
      <td>O(1)</td>
      <td>O(n)</td>
      <td>-</td>
    </tr>
    <tr>
      <td>Search</td>
      <td>O(1)</td>
      <td>O(n)</td>
      <td>-</td>
    </tr>
    <tr>
      <td>Get</td>
      <td>O(1)</td>
      <td>O(n)</td>
      <td>-</td>
    </tr>
  </tbody>
</table>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="rouge-code"><pre><span class="c1"># Creation
</span><span class="n">d</span> <span class="o">=</span> <span class="p">{</span><span class="sh">'</span><span class="s">a</span><span class="sh">'</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="sh">'</span><span class="s">b</span><span class="sh">'</span><span class="p">:</span> <span class="mi">2</span><span class="p">}</span>
<span class="n">d</span> <span class="o">=</span> <span class="nf">dict</span><span class="p">(</span><span class="n">a</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
<span class="n">d</span> <span class="o">=</span> <span class="nf">dict</span><span class="p">(</span><span class="nf">zip</span><span class="p">(</span><span class="n">keys</span><span class="p">,</span> <span class="n">values</span><span class="p">))</span>

<span class="c1"># Access
</span><span class="n">d</span><span class="p">[</span><span class="sh">'</span><span class="s">key</span><span class="sh">'</span><span class="p">]</span>              <span class="c1"># O(1) - raises KeyError if missing
</span><span class="n">d</span><span class="p">.</span><span class="nf">get</span><span class="p">(</span><span class="sh">'</span><span class="s">key</span><span class="sh">'</span><span class="p">)</span>          <span class="c1"># O(1) - returns None
</span><span class="n">d</span><span class="p">.</span><span class="nf">get</span><span class="p">(</span><span class="sh">'</span><span class="s">key</span><span class="sh">'</span><span class="p">,</span> <span class="n">default</span><span class="p">)</span> <span class="c1"># O(1) - returns default
</span>
<span class="c1"># Add/Update
</span><span class="n">d</span><span class="p">[</span><span class="sh">'</span><span class="s">key</span><span class="sh">'</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>     <span class="c1"># O(1)
</span><span class="n">d</span><span class="p">.</span><span class="nf">update</span><span class="p">({</span><span class="sh">'</span><span class="s">k1</span><span class="sh">'</span><span class="p">:</span> <span class="n">v1</span><span class="p">,</span> <span class="sh">'</span><span class="s">k2</span><span class="sh">'</span><span class="p">:</span> <span class="n">v2</span><span class="p">})</span>  <span class="c1"># O(k)
</span>
<span class="c1"># Remove
</span><span class="k">del</span> <span class="n">d</span><span class="p">[</span><span class="sh">'</span><span class="s">key</span><span class="sh">'</span><span class="p">]</span>          <span class="c1"># O(1)
</span><span class="n">value</span> <span class="o">=</span> <span class="n">d</span><span class="p">.</span><span class="nf">pop</span><span class="p">(</span><span class="sh">'</span><span class="s">key</span><span class="sh">'</span><span class="p">)</span> <span class="c1"># O(1)
</span><span class="n">d</span><span class="p">.</span><span class="nf">popitem</span><span class="p">()</span>           <span class="c1"># O(1) - removes last item
</span>
<span class="c1"># defaultdict
</span><span class="kn">from</span> <span class="n">collections</span> <span class="kn">import</span> <span class="n">defaultdict</span>
<span class="n">dd</span> <span class="o">=</span> <span class="nf">defaultdict</span><span class="p">(</span><span class="nb">list</span><span class="p">)</span>
<span class="n">dd</span><span class="p">[</span><span class="sh">'</span><span class="s">key</span><span class="sh">'</span><span class="p">].</span><span class="nf">append</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>

<span class="c1"># Merge (Python 3.9+)
</span><span class="n">d3</span> <span class="o">=</span> <span class="n">d1</span> <span class="o">|</span> <span class="n">d2</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p><strong>Interview Questions:</strong></p>

<p><strong>Q: How is dictionary implemented in Python?</strong><br />
A: Python dict uses a hash table with open addressing. Uses hash to compute index, handles collisions via probing.</p>

<p><strong>Q: What is hash collision and how does Python handle it?</strong><br />
A: When two keys hash to same index. Python uses open addressing - probes for next empty slot.</p>

<p><strong>Q: Difference between <code class="language-plaintext highlighter-rouge">get()</code> and <code class="language-plaintext highlighter-rouge">[]</code>?</strong><br />
A: get() returns None/default if key not found. [] raises KeyError.</p>

<p><strong>Q: How does dictionary maintain insertion order (Python 3.7+)?</strong><br />
A: Python 3.7+ maintains insertion order using compact array.</p>

<p><strong>Q: What is the difference between dict and collections.defaultdict?</strong><br />
A: Regular dict raises KeyError for missing keys. defaultdict provides default values.</p>

<p><strong>Q: How would you merge two dictionaries in Python?</strong><br />
A: Use | operator (Python 3.9+), update(), or {**d1, **d2}.</p>

<hr />

<h3 id="when-to-use-the-array-module"><a name="array-module"></a>When to use the Array module?</h3>
<p><strong>Definition:</strong> <strong>Homogeneous array</strong> of numeric values stored more <strong>efficiently</strong> than lists.</p>

<table>
  <thead>
    <tr>
      <th>Operation</th>
      <th>Time</th>
      <th>Space</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Access by index</td>
      <td>O(1)</td>
      <td>-</td>
    </tr>
    <tr>
      <td>Append</td>
      <td>O(1)*</td>
      <td>-</td>
    </tr>
    <tr>
      <td>Insert</td>
      <td>O(n)</td>
      <td>-</td>
    </tr>
    <tr>
      <td>Delete</td>
      <td>O(n)</td>
      <td>-</td>
    </tr>
  </tbody>
</table>

<p>*Amortized</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
</pre></td><td class="rouge-code"><pre><span class="kn">from</span> <span class="n">array</span> <span class="kn">import</span> <span class="n">array</span>

<span class="c1"># Type codes: 'b','B','h','H','i','I','f','d'
</span><span class="n">arr</span> <span class="o">=</span> <span class="nf">array</span><span class="p">(</span><span class="sh">'</span><span class="s">i</span><span class="sh">'</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>
<span class="n">arr</span><span class="p">.</span><span class="nf">append</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>    <span class="c1"># O(1)
</span><span class="n">arr</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>           <span class="c1"># O(1)
</span></pre></td></tr></tbody></table></code></pre></div></div>

<hr />

<h3 id="what-advanced-structures-does-the-collections-module-offer"><a name="collections"></a>What advanced structures does the Collections Module offer?</h3>

<p><strong>deque:</strong> Double-ended queue - O(1) add/remove at both ends</p>

<table>
  <thead>
    <tr>
      <th>Operation</th>
      <th>Time</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>appendleft</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>appendright</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>popleft</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>popright</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>rotate</td>
      <td>O(k)</td>
    </tr>
  </tbody>
</table>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
</pre></td><td class="rouge-code"><pre><span class="kn">from</span> <span class="n">collections</span> <span class="kn">import</span> <span class="n">deque</span>
<span class="n">dq</span> <span class="o">=</span> <span class="nf">deque</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>
<span class="n">dq</span><span class="p">.</span><span class="nf">appendleft</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>      <span class="c1"># O(1)
</span><span class="n">dq</span><span class="p">.</span><span class="nf">appendright</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>    <span class="c1"># O(1)
</span><span class="n">dq</span><span class="p">.</span><span class="nf">popleft</span><span class="p">()</span>         <span class="c1"># O(1)
</span><span class="n">dq</span><span class="p">.</span><span class="nf">rotate</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>          <span class="c1"># O(k)
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p><strong>Counter:</strong> Count hashable items</p>

<table>
  <thead>
    <tr>
      <th>Operation</th>
      <th>Time</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Access</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>Most common</td>
      <td>O(n log k)</td>
    </tr>
  </tbody>
</table>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
</pre></td><td class="rouge-code"><pre><span class="kn">from</span> <span class="n">collections</span> <span class="kn">import</span> <span class="n">Counter</span>
<span class="n">c</span> <span class="o">=</span> <span class="nc">Counter</span><span class="p">([</span><span class="sh">'</span><span class="s">a</span><span class="sh">'</span><span class="p">,</span> <span class="sh">'</span><span class="s">b</span><span class="sh">'</span><span class="p">,</span> <span class="sh">'</span><span class="s">a</span><span class="sh">'</span><span class="p">,</span> <span class="sh">'</span><span class="s">c</span><span class="sh">'</span><span class="p">,</span> <span class="sh">'</span><span class="s">a</span><span class="sh">'</span><span class="p">])</span>
<span class="n">c</span><span class="p">[</span><span class="sh">'</span><span class="s">a</span><span class="sh">'</span><span class="p">]</span>                <span class="c1"># O(1) - 3
</span><span class="n">c</span><span class="p">.</span><span class="nf">most_common</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>     <span class="c1"># O(n log k)
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p><strong>namedtuple:</strong> Tuple with named fields</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
</pre></td><td class="rouge-code"><pre><span class="kn">from</span> <span class="n">collections</span> <span class="kn">import</span> <span class="n">namedtuple</span>
<span class="n">Point</span> <span class="o">=</span> <span class="nf">namedtuple</span><span class="p">(</span><span class="sh">'</span><span class="s">Point</span><span class="sh">'</span><span class="p">,</span> <span class="p">[</span><span class="sh">'</span><span class="s">x</span><span class="sh">'</span><span class="p">,</span> <span class="sh">'</span><span class="s">y</span><span class="sh">'</span><span class="p">])</span>
<span class="n">p</span> <span class="o">=</span> <span class="nc">Point</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="n">p</span><span class="p">.</span><span class="n">x</span><span class="p">,</span> <span class="n">p</span><span class="p">.</span><span class="n">y</span>             <span class="c1"># O(1)
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p><strong>defaultdict:</strong> Dict with default values - same complexity as dict</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
</pre></td><td class="rouge-code"><pre><span class="kn">from</span> <span class="n">collections</span> <span class="kn">import</span> <span class="n">defaultdict</span>
<span class="n">dd</span> <span class="o">=</span> <span class="nf">defaultdict</span><span class="p">(</span><span class="nb">int</span><span class="p">)</span>
<span class="n">dd</span><span class="p">[</span><span class="sh">'</span><span class="s">key</span><span class="sh">'</span><span class="p">].</span><span class="nf">append</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>  <span class="c1"># O(1)
</span></pre></td></tr></tbody></table></code></pre></div></div>

<hr />

<h2 id="what-does-the-c-stl-provide"><a name="cpp"></a>What does the C++ (STL) provide?</h2>

<h3 id="how-do-you-use-a-vector-vector"><a name="vector"></a>How do you use a Vector <code class="language-plaintext highlighter-rouge">&lt;vector&gt;</code>?</h3>
<p><strong>Definition:</strong> <strong>Dynamic array</strong> that can resize automatically. Provides <strong>random access</strong>.</p>

<table>
  <thead>
    <tr>
      <th>Operation</th>
      <th>Time</th>
      <th>Space</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Access by index</td>
      <td>O(1)</td>
      <td>-</td>
    </tr>
    <tr>
      <td>push_back</td>
      <td>O(1)*</td>
      <td>-</td>
    </tr>
    <tr>
      <td>pop_back</td>
      <td>O(1)</td>
      <td>-</td>
    </tr>
    <tr>
      <td>insert at position</td>
      <td>O(n)</td>
      <td>-</td>
    </tr>
    <tr>
      <td>erase at position</td>
      <td>O(n)</td>
      <td>-</td>
    </tr>
    <tr>
      <td>Search</td>
      <td>O(n)</td>
      <td>-</td>
    </tr>
    <tr>
      <td>Sort</td>
      <td>O(n log n)</td>
      <td>O(n)</td>
    </tr>
  </tbody>
</table>

<p>*Amortized</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="rouge-code"><pre><span class="cp">#include</span> <span class="cpf">&lt;vector&gt;</span><span class="cp">
</span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>

<span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">v</span><span class="p">;</span>
<span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">v</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">};</span>
<span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">v</span><span class="p">(</span><span class="mi">5</span><span class="p">);</span>
<span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">v</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">10</span><span class="p">);</span>

<span class="n">v</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">v</span><span class="p">.</span><span class="n">at</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>           <span class="c1">// O(1)</span>
<span class="n">v</span><span class="p">.</span><span class="n">front</span><span class="p">(),</span> <span class="n">v</span><span class="p">.</span><span class="n">back</span><span class="p">()</span>     <span class="c1">// O(1)</span>
<span class="n">v</span><span class="p">.</span><span class="n">push_back</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>         <span class="c1">// O(1)*</span>
<span class="n">v</span><span class="p">.</span><span class="n">pop_back</span><span class="p">();</span>           <span class="c1">// O(1)</span>
<span class="n">v</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span><span class="n">it</span><span class="p">,</span> <span class="n">x</span><span class="p">);</span>        <span class="c1">// O(n)</span>
<span class="n">v</span><span class="p">.</span><span class="n">erase</span><span class="p">(</span><span class="n">it</span><span class="p">);</span>            <span class="c1">// O(n)</span>
<span class="n">v</span><span class="p">.</span><span class="n">size</span><span class="p">(),</span> <span class="n">v</span><span class="p">.</span><span class="n">empty</span><span class="p">()</span>     <span class="c1">// O(1)</span>
<span class="n">v</span><span class="p">.</span><span class="n">capacity</span><span class="p">()</span>           <span class="c1">// O(1)</span>
<span class="n">v</span><span class="p">.</span><span class="n">reserve</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>          <span class="c1">// O(n)</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p><strong>Interview Questions:</strong></p>

<p><strong>Q: What is amortized O(1) for push_back?</strong><br />
A: Most push_back calls are O(1), but occasionally reallocates and copies all elements. This rare cost is amortized.</p>

<p><strong>Q: Difference between vector and array?</strong><br />
A: Array has fixed size at compile time. Vector has dynamic size.</p>

<p><strong>Q: When should you use reserve()?</strong><br />
A: When you know approximate size beforehand. Prevents multiple reallocations.</p>

<p><strong>Q: Capacity vs size?</strong><br />
A: size = elements. capacity = memory allocated. size &lt;= capacity.</p>

<hr />

<h3 id="how-does-a-deque-deque-operate"><a name="deque"></a>How does a Deque <code class="language-plaintext highlighter-rouge">&lt;deque&gt;</code> operate?</h3>
<p><strong>Definition:</strong> <strong>Double-ended queue</strong> allowing <strong>O(1) insertion/deletion</strong> at both front and back.</p>

<table>
  <thead>
    <tr>
      <th>Operation</th>
      <th>Time</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>push_front</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>push_back</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>pop_front</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>pop_back</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>Access</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>Insert at position</td>
      <td>O(n)</td>
    </tr>
  </tbody>
</table>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
</pre></td><td class="rouge-code"><pre><span class="cp">#include</span> <span class="cpf">&lt;deque&gt;</span><span class="cp">
</span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>

<span class="n">deque</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">dq</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">};</span>
<span class="n">dq</span><span class="p">.</span><span class="n">push_front</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>      <span class="c1">// O(1)</span>
<span class="n">dq</span><span class="p">.</span><span class="n">push_back</span><span class="p">(</span><span class="mi">4</span><span class="p">);</span>       <span class="c1">// O(1)</span>
<span class="n">dq</span><span class="p">.</span><span class="n">pop_front</span><span class="p">();</span>        <span class="c1">// O(1)</span>
<span class="n">dq</span><span class="p">.</span><span class="n">pop_back</span><span class="p">();</span>         <span class="c1">// O(1)</span>
<span class="n">dq</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">dq</span><span class="p">.</span><span class="n">at</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>       <span class="c1">// O(1)</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p><strong>Interview Questions:</strong></p>

<p><strong>Q: How is deque different from vector?</strong><br />
A: Deque allows O(1) at both ends. Vector only at back.</p>

<p><strong>Q: Internal structure?</strong><br />
A: Uses multiple fixed-size arrays (chunks) mapped together.</p>

<hr />

<h3 id="when-is-a-list-list-useful-in-c"><a name="list-cpp"></a>When is a List <code class="language-plaintext highlighter-rouge">&lt;list&gt;</code> useful in C++?</h3>
<p><strong>Definition:</strong> <strong>Doubly linked list</strong> with <strong>O(1) insertion/deletion</strong> anywhere. No random access.</p>

<table>
  <thead>
    <tr>
      <th>Operation</th>
      <th>Time</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>push_front</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>push_back</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>pop_front</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>pop_back</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>insert at iterator</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>erase at iterator</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>Access by index</td>
      <td>O(n)</td>
    </tr>
    <tr>
      <td>Search</td>
      <td>O(n)</td>
    </tr>
  </tbody>
</table>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
</pre></td><td class="rouge-code"><pre><span class="cp">#include</span> <span class="cpf">&lt;list&gt;</span><span class="cp">
</span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>

<span class="n">list</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">lst</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">};</span>
<span class="n">lst</span><span class="p">.</span><span class="n">push_front</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>           <span class="c1">// O(1)</span>
<span class="n">lst</span><span class="p">.</span><span class="n">push_back</span><span class="p">(</span><span class="mi">4</span><span class="p">);</span>            <span class="c1">// O(1)</span>
<span class="n">lst</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span><span class="o">++</span><span class="n">lst</span><span class="p">.</span><span class="n">begin</span><span class="p">(),</span> <span class="mi">5</span><span class="p">);</span> <span class="c1">// O(1) with iterator</span>
<span class="n">lst</span><span class="p">.</span><span class="n">remove</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span>               <span class="c1">// O(n)</span>
<span class="n">lst</span><span class="p">.</span><span class="n">reverse</span><span class="p">();</span>              <span class="c1">// O(n)</span>
<span class="n">lst</span><span class="p">.</span><span class="n">sort</span><span class="p">();</span>                 <span class="c1">// O(n log n)</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p><strong>Interview Questions:</strong></p>

<p><strong>Q: When to use list over vector?</strong><br />
A: When frequent insertion/deletion in middle, or at front.</p>

<p><strong>Q: Time complexity of inserting in middle?</strong><br />
A: O(1) if you have iterator. O(n) to find position.</p>

<hr />

<h3 id="how-to-use-a-fixed-size-array-array"><a name="array-cpp"></a>How to use a fixed-size Array <code class="language-plaintext highlighter-rouge">&lt;array&gt;</code>?</h3>
<p><strong>Definition:</strong> <strong>Fixed-size container</strong> wrapping C-style array.</p>

<table>
  <thead>
    <tr>
      <th>Operation</th>
      <th>Time</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Access</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>fill</td>
      <td>O(n)</td>
    </tr>
  </tbody>
</table>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
</pre></td><td class="rouge-code"><pre><span class="cp">#include</span> <span class="cpf">&lt;array&gt;</span><span class="cp">
</span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>

<span class="n">array</span><span class="o">&lt;</span><span class="kt">int</span><span class="p">,</span> <span class="mi">5</span><span class="o">&gt;</span> <span class="n">arr</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">};</span>
<span class="n">arr</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">arr</span><span class="p">.</span><span class="n">at</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>      <span class="c1">// O(1)</span>
<span class="n">arr</span><span class="p">.</span><span class="n">fill</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>          <span class="c1">// O(n)</span>
<span class="n">arr</span><span class="p">.</span><span class="n">size</span><span class="p">()</span>            <span class="c1">// O(1) - compile time</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<hr />

<h3 id="how-do-you-implement-a-stack-stack"><a name="stack"></a>How do you implement a Stack <code class="language-plaintext highlighter-rouge">&lt;stack&gt;</code>?</h3>
<p><strong>Definition:</strong> <strong>LIFO adapter</strong> with push, pop, top operations.</p>

<table>
  <thead>
    <tr>
      <th>Operation</th>
      <th>Time</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>push</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>pop</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>top</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>empty</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>size</td>
      <td>O(1)</td>
    </tr>
  </tbody>
</table>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
</pre></td><td class="rouge-code"><pre><span class="cp">#include</span> <span class="cpf">&lt;stack&gt;</span><span class="cp">
</span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>

<span class="n">stack</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">st</span><span class="p">;</span>
<span class="n">st</span><span class="p">.</span><span class="n">push</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>         <span class="c1">// O(1)</span>
<span class="n">st</span><span class="p">.</span><span class="n">top</span><span class="p">();</span>          <span class="c1">// O(1)</span>
<span class="n">st</span><span class="p">.</span><span class="n">pop</span><span class="p">();</span>          <span class="c1">// O(1)</span>
<span class="n">st</span><span class="p">.</span><span class="n">empty</span><span class="p">();</span>        <span class="c1">// O(1)</span>
<span class="n">st</span><span class="p">.</span><span class="n">size</span><span class="p">();</span>         <span class="c1">// O(1)</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p><strong>Interview Questions:</strong></p>

<p><strong>Q: Basic operations of stack?</strong><br />
A: push, pop, top, empty, size - all O(1).</p>

<p><strong>Q: Applications of stack?</strong><br />
A: DFS, expression evaluation, parenthesis matching, undo/redo.</p>

<hr />

<h3 id="how-to-manage-fifo-with-a-queue-queue"><a name="queue"></a>How to manage FIFO with a Queue <code class="language-plaintext highlighter-rouge">&lt;queue&gt;</code>?</h3>
<p><strong>Definition:</strong> <strong>FIFO adapter</strong> with enqueue at back, dequeue at front.</p>

<table>
  <thead>
    <tr>
      <th>Operation</th>
      <th>Time</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>push</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>pop</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>front</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>back</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>empty</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>size</td>
      <td>O(1)</td>
    </tr>
  </tbody>
</table>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
</pre></td><td class="rouge-code"><pre><span class="cp">#include</span> <span class="cpf">&lt;queue&gt;</span><span class="cp">
</span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>

<span class="n">queue</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">q</span><span class="p">;</span>
<span class="n">q</span><span class="p">.</span><span class="n">push</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>          <span class="c1">// O(1)</span>
<span class="n">q</span><span class="p">.</span><span class="n">front</span><span class="p">();</span>         <span class="c1">// O(1)</span>
<span class="n">q</span><span class="p">.</span><span class="n">back</span><span class="p">();</span>          <span class="c1">// O(1)</span>
<span class="n">q</span><span class="p">.</span><span class="n">pop</span><span class="p">();</span>           <span class="c1">// O(1)</span>
<span class="n">q</span><span class="p">.</span><span class="n">empty</span><span class="p">();</span>         <span class="c1">// O(1)</span>
<span class="n">q</span><span class="p">.</span><span class="n">size</span><span class="p">();</span>          <span class="c1">// O(1)</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p><strong>Interview Questions:</strong></p>

<p><strong>Q: Applications of queue?</strong><br />
A: BFS, task scheduling, print job queue.</p>

<hr />

<h3 id="when-to-use-a-priority-queue-queue"><a name="priority-queue"></a>When to use a Priority Queue <code class="language-plaintext highlighter-rouge">&lt;queue&gt;</code>?</h3>
<p><strong>Definition:</strong> <strong>Heap-based</strong>, max/min element at top.</p>

<table>
  <thead>
    <tr>
      <th>Operation</th>
      <th>Time</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>push</td>
      <td>O(log n)</td>
    </tr>
    <tr>
      <td>pop</td>
      <td>O(log n)</td>
    </tr>
    <tr>
      <td>top</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>empty</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>size</td>
      <td>O(1)</td>
    </tr>
  </tbody>
</table>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
</pre></td><td class="rouge-code"><pre><span class="cp">#include</span> <span class="cpf">&lt;queue&gt;</span><span class="cp">
</span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>

<span class="n">priority_queue</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">pq</span><span class="p">;</span>
<span class="n">priority_queue</span><span class="o">&lt;</span><span class="kt">int</span><span class="p">,</span> <span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="p">,</span> <span class="n">greater</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;&gt;</span> <span class="n">pq</span><span class="p">;</span>

<span class="n">pq</span><span class="p">.</span><span class="n">push</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>        <span class="c1">// O(log n)</span>
<span class="n">pq</span><span class="p">.</span><span class="n">top</span><span class="p">();</span>         <span class="c1">// O(1)</span>
<span class="n">pq</span><span class="p">.</span><span class="n">pop</span><span class="p">();</span>         <span class="c1">// O(log n)</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p><strong>Interview Questions:</strong></p>

<p><strong>Q: Time complexity of push and pop?</strong><br />
A: Both O(log n).</p>

<p><strong>Q: Applications?</strong><br />
A: Dijkstra’s, Huffman coding, top-k elements.</p>

<hr />

<h3 id="how-does-a-set-set-maintain-order"><a name="set-cpp"></a>How does a Set <code class="language-plaintext highlighter-rouge">&lt;set&gt;</code> maintain order?</h3>
<p><strong>Definition:</strong> <strong>Sorted unique elements</strong>. Implemented as a <strong>Red-Black tree</strong>.</p>

<table>
  <thead>
    <tr>
      <th>Operation</th>
      <th>Time</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>insert</td>
      <td>O(log n)</td>
    </tr>
    <tr>
      <td>erase</td>
      <td>O(log n)</td>
    </tr>
    <tr>
      <td>find</td>
      <td>O(log n)</td>
    </tr>
    <tr>
      <td>count</td>
      <td>O(log n)</td>
    </tr>
    <tr>
      <td>lower_bound</td>
      <td>O(log n)</td>
    </tr>
    <tr>
      <td>upper_bound</td>
      <td>O(log n)</td>
    </tr>
  </tbody>
</table>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
</pre></td><td class="rouge-code"><pre><span class="cp">#include</span> <span class="cpf">&lt;set&gt;</span><span class="cp">
</span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>

<span class="n">set</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">s</span> <span class="o">=</span> <span class="p">{</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">9</span><span class="p">};</span>
<span class="n">s</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>                    <span class="c1">// O(log n)</span>
<span class="n">s</span><span class="p">.</span><span class="n">erase</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>                     <span class="c1">// O(log n)</span>
<span class="n">s</span><span class="p">.</span><span class="n">find</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">!=</span> <span class="n">s</span><span class="p">.</span><span class="n">end</span><span class="p">()</span>           <span class="c1">// O(log n)</span>
<span class="n">s</span><span class="p">.</span><span class="n">count</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>                      <span class="c1">// O(log n)</span>
<span class="n">s</span><span class="p">.</span><span class="n">lower_bound</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>              <span class="c1">// O(log n)</span>
<span class="n">s</span><span class="p">.</span><span class="n">upper_bound</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>              <span class="c1">// O(log n)</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p><strong>Interview Questions:</strong></p>

<p><strong>Q: Time complexity?</strong><br />
A: All operations O(log n) due to tree structure.</p>

<hr />

<h3 id="what-is-a-multiset-multiset"><a name="multiset"></a>What is a Multiset <code class="language-plaintext highlighter-rouge">&lt;multiset&gt;</code>?</h3>
<p><strong>Definition:</strong> <strong>Sorted container</strong> allowing <strong>duplicate elements</strong>.</p>

<table>
  <thead>
    <tr>
      <th>Operation</th>
      <th>Time</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>insert</td>
      <td>O(log n)</td>
    </tr>
    <tr>
      <td>erase</td>
      <td>O(log n)*</td>
    </tr>
    <tr>
      <td>find</td>
      <td>O(log n)</td>
    </tr>
    <tr>
      <td>count</td>
      <td>O(log n + k)</td>
    </tr>
  </tbody>
</table>

<p>*Each occurrence</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
</pre></td><td class="rouge-code"><pre><span class="cp">#include</span> <span class="cpf">&lt;set&gt;</span><span class="cp">
</span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>

<span class="n">multiset</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">ms</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">};</span>
<span class="n">ms</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span>       <span class="c1">// O(log n)</span>
<span class="n">ms</span><span class="p">.</span><span class="n">count</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span>      <span class="c1">// O(log n + k)</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<hr />

<h3 id="how-does-a-map-map-handle-key-value-pairs"><a name="map"></a>How does a Map <code class="language-plaintext highlighter-rouge">&lt;map&gt;</code> handle key-value pairs?</h3>
<p><strong>Definition:</strong> <strong>Sorted key-value pairs</strong>. Implemented as a <strong>Red-Black tree</strong>.</p>

<table>
  <thead>
    <tr>
      <th>Operation</th>
      <th>Time</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>insert</td>
      <td>O(log n)</td>
    </tr>
    <tr>
      <td>erase</td>
      <td>O(log n)</td>
    </tr>
    <tr>
      <td>find</td>
      <td>O(log n)</td>
    </tr>
    <tr>
      <td>access by key</td>
      <td>O(log n)</td>
    </tr>
    <tr>
      <td>operator[]</td>
      <td>O(log n)</td>
    </tr>
    <tr>
      <td>at()</td>
      <td>O(log n)</td>
    </tr>
  </tbody>
</table>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
</pre></td><td class="rouge-code"><pre><span class="cp">#include</span> <span class="cpf">&lt;map&gt;</span><span class="cp">
</span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>

<span class="n">map</span><span class="o">&lt;</span><span class="n">string</span><span class="p">,</span> <span class="kt">int</span><span class="o">&gt;</span> <span class="n">m</span><span class="p">;</span>
<span class="n">m</span><span class="p">[</span><span class="s">"key"</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span><span class="p">;</span>              <span class="c1">// O(log n)</span>
<span class="n">m</span><span class="p">.</span><span class="n">at</span><span class="p">(</span><span class="s">"key"</span><span class="p">);</span>                   <span class="c1">// O(log n)</span>
<span class="n">m</span><span class="p">.</span><span class="n">insert</span><span class="p">({</span><span class="s">"key"</span><span class="p">,</span> <span class="n">value</span><span class="p">});</span>     <span class="c1">// O(log n)</span>
<span class="n">m</span><span class="p">.</span><span class="n">find</span><span class="p">(</span><span class="s">"key"</span><span class="p">)</span> <span class="o">!=</span> <span class="n">m</span><span class="p">.</span><span class="n">end</span><span class="p">()</span>      <span class="c1">// O(log n)</span>
<span class="n">m</span><span class="p">.</span><span class="n">erase</span><span class="p">(</span><span class="s">"key"</span><span class="p">);</span>                <span class="c1">// O(log n)</span>
<span class="k">for</span><span class="p">(</span><span class="k">auto</span> <span class="p">[</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">]</span> <span class="o">:</span> <span class="n">m</span><span class="p">)</span> <span class="p">{}</span>       <span class="c1">// O(n)</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p><strong>Interview Questions:</strong></p>

<p><strong>Q: vs unordered_map?</strong><br />
A: map is O(log n) sorted. unordered_map is O(1) average.</p>

<hr />

<h3 id="when-is-a-multimap-multimap-useful"><a name="multimap"></a>When is a Multimap <code class="language-plaintext highlighter-rouge">&lt;multimap&gt;</code> useful?</h3>
<p><strong>Definition:</strong> <strong>Sorted key-value pairs</strong> allowing <strong>duplicate keys</strong>.</p>

<table>
  <thead>
    <tr>
      <th>Operation</th>
      <th>Time</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>insert</td>
      <td>O(log n)</td>
    </tr>
    <tr>
      <td>erase</td>
      <td>O(log n)</td>
    </tr>
    <tr>
      <td>find</td>
      <td>O(log n)</td>
    </tr>
    <tr>
      <td>equal_range</td>
      <td>O(log n + k)</td>
    </tr>
  </tbody>
</table>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
</pre></td><td class="rouge-code"><pre><span class="cp">#include</span> <span class="cpf">&lt;map&gt;</span><span class="cp">
</span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>

<span class="n">multimap</span><span class="o">&lt;</span><span class="n">string</span><span class="p">,</span> <span class="kt">int</span><span class="o">&gt;</span> <span class="n">mm</span><span class="p">;</span>
<span class="n">mm</span><span class="p">.</span><span class="n">insert</span><span class="p">({</span><span class="s">"fruit"</span><span class="p">,</span> <span class="mi">1</span><span class="p">});</span>       <span class="c1">// O(log n)</span>
<span class="n">mm</span><span class="p">.</span><span class="n">insert</span><span class="p">({</span><span class="s">"fruit"</span><span class="p">,</span> <span class="mi">2</span><span class="p">});</span>      <span class="c1">// O(log n)</span>
<span class="n">mm</span><span class="p">.</span><span class="n">equal_range</span><span class="p">(</span><span class="s">"fruit"</span><span class="p">);</span>     <span class="c1">// O(log n + k)</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<hr />

<h3 id="how-does-an-unordered-set-unordered_set-improve-lookup"><a name="unordered-set"></a>How does an Unordered Set <code class="language-plaintext highlighter-rouge">&lt;unordered_set&gt;</code> improve lookup?</h3>
<p><strong>Definition:</strong> <strong>Hash-based set</strong>. Provides <strong>O(1) average lookup</strong>.</p>

<table>
  <thead>
    <tr>
      <th>Operation</th>
      <th>Time (Avg)</th>
      <th>Time (Worst)</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>insert</td>
      <td>O(1)</td>
      <td>O(n)</td>
    </tr>
    <tr>
      <td>erase</td>
      <td>O(1)</td>
      <td>O(n)</td>
    </tr>
    <tr>
      <td>find</td>
      <td>O(1)</td>
      <td>O(n)</td>
    </tr>
    <tr>
      <td>count</td>
      <td>O(1)</td>
      <td>O(n)</td>
    </tr>
  </tbody>
</table>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
</pre></td><td class="rouge-code"><pre><span class="cp">#include</span> <span class="cpf">&lt;unordered_set&gt;</span><span class="cp">
</span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>

<span class="n">unordered_set</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">us</span> <span class="o">=</span> <span class="p">{</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">};</span>
<span class="n">us</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>                  <span class="c1">// O(1) avg</span>
<span class="n">us</span><span class="p">.</span><span class="n">find</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">!=</span> <span class="n">us</span><span class="p">.</span><span class="n">end</span><span class="p">()</span>       <span class="c1">// O(1) avg</span>
<span class="n">us</span><span class="p">.</span><span class="n">erase</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>                  <span class="c1">// O(1) avg</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p><strong>Interview Questions:</strong></p>

<p><strong>Q: Load factor?</strong><br />
A: Ratio of elements to buckets. Lower = less collisions, more memory.</p>

<hr />

<h3 id="how-fast-is-an-unordered-map-unordered_map"><a name="unordered-map"></a>How fast is an Unordered Map <code class="language-plaintext highlighter-rouge">&lt;unordered_map&gt;</code>?</h3>
<p><strong>Definition:</strong> <strong>Hash-based map</strong>. Provides <strong>O(1) average lookup</strong>.</p>

<table>
  <thead>
    <tr>
      <th>Operation</th>
      <th>Time (Avg)</th>
      <th>Time (Worst)</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>insert</td>
      <td>O(1)</td>
      <td>O(n)</td>
    </tr>
    <tr>
      <td>erase</td>
      <td>O(1)</td>
      <td>O(n)</td>
    </tr>
    <tr>
      <td>find</td>
      <td>O(1)</td>
      <td>O(n)</td>
    </tr>
    <tr>
      <td>access</td>
      <td>O(1)</td>
      <td>O(n)</td>
    </tr>
  </tbody>
</table>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
</pre></td><td class="rouge-code"><pre><span class="cp">#include</span> <span class="cpf">&lt;unordered_map&gt;</span><span class="cp">
</span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>

<span class="n">unordered_map</span><span class="o">&lt;</span><span class="n">string</span><span class="p">,</span> <span class="kt">int</span><span class="o">&gt;</span> <span class="n">um</span><span class="p">;</span>
<span class="n">um</span><span class="p">[</span><span class="s">"key"</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span><span class="p">;</span>            <span class="c1">// O(1) avg</span>
<span class="n">um</span><span class="p">.</span><span class="n">at</span><span class="p">(</span><span class="s">"key"</span><span class="p">);</span>                 <span class="c1">// O(1) avg</span>
<span class="n">um</span><span class="p">.</span><span class="n">find</span><span class="p">(</span><span class="s">"key"</span><span class="p">)</span> <span class="o">!=</span> <span class="n">um</span><span class="p">.</span><span class="n">end</span><span class="p">()</span>  <span class="c1">// O(1) avg</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p><strong>Interview Questions:</strong></p>

<p><strong>Q: Hash collisions?</strong><br />
A: Uses chaining or probing. Falls back to rehashing.</p>

<hr />

<h3 id="how-do-you-use-pair--tuple-in-c"><a name="pair-tuple"></a>How do you use Pair &amp; Tuple in C++?</h3>

<table>
  <thead>
    <tr>
      <th>Operation</th>
      <th>Time</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Access</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>Make</td>
      <td>O(1)</td>
    </tr>
  </tbody>
</table>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
</pre></td><td class="rouge-code"><pre><span class="cp">#include</span> <span class="cpf">&lt;utility&gt;</span><span class="cp">
#include</span> <span class="cpf">&lt;tuple&gt;</span><span class="cp">
</span>
<span class="n">pair</span><span class="o">&lt;</span><span class="kt">int</span><span class="p">,</span> <span class="n">string</span><span class="o">&gt;</span> <span class="n">p</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="s">"hello"</span><span class="p">};</span>
<span class="n">p</span><span class="p">.</span><span class="n">first</span><span class="p">,</span> <span class="n">p</span><span class="p">.</span><span class="n">second</span>              <span class="c1">// O(1)</span>

<span class="n">tuple</span><span class="o">&lt;</span><span class="kt">int</span><span class="p">,</span> <span class="n">string</span><span class="p">,</span> <span class="kt">double</span><span class="o">&gt;</span> <span class="n">t</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="s">"hi"</span><span class="p">,</span> <span class="mf">2.5</span><span class="p">};</span>
<span class="n">get</span><span class="o">&lt;</span><span class="mi">0</span><span class="o">&gt;</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>                     <span class="c1">// O(1)</span>
<span class="n">tie</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span><span class="p">)</span> <span class="o">=</span> <span class="n">t</span><span class="p">;</span>            <span class="c1">// O(k)</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<hr />

<h3 id="how-do-you-manipulate-a-string-string-in-c"><a name="string-cpp"></a>How do you manipulate a String <code class="language-plaintext highlighter-rouge">&lt;string&gt;</code> in C++?</h3>

<table>
  <thead>
    <tr>
      <th>Operation</th>
      <th>Time</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Access</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>Concatenate</td>
      <td>O(n+m)</td>
    </tr>
    <tr>
      <td>Substring</td>
      <td>O(m)</td>
    </tr>
    <tr>
      <td>Find</td>
      <td>O(n*m)</td>
    </tr>
    <tr>
      <td>Replace</td>
      <td>O(n+m)</td>
    </tr>
    <tr>
      <td>Size/Length</td>
      <td>O(1)</td>
    </tr>
  </tbody>
</table>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
</pre></td><td class="rouge-code"><pre><span class="cp">#include</span> <span class="cpf">&lt;string&gt;</span><span class="cp">
</span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>

<span class="n">string</span> <span class="n">s</span> <span class="o">=</span> <span class="s">"hello"</span><span class="p">;</span>
<span class="n">s</span> <span class="o">+=</span> <span class="s">" world"</span><span class="p">;</span>               <span class="c1">// O(m) amortized</span>
<span class="n">s</span><span class="p">.</span><span class="n">size</span><span class="p">(),</span> <span class="n">s</span><span class="p">.</span><span class="n">length</span><span class="p">()</span>        <span class="c1">// O(1)</span>
<span class="n">s</span><span class="p">.</span><span class="n">find</span><span class="p">(</span><span class="s">"lo"</span><span class="p">)</span>               <span class="c1">// O(n*m)</span>
<span class="n">s</span><span class="p">.</span><span class="n">substr</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>            <span class="c1">// O(m)</span>
<span class="n">s</span><span class="p">.</span><span class="n">replace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="s">"bye"</span><span class="p">)</span>     <span class="c1">// O(n+m)</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<hr />

<h2 id="what-data-structures-exist-in-c-language"><a name="c-lang"></a>What Data Structures exist in C Language?</h2>

<h3 id="how-do-c-arrays-work"><a name="array-c"></a>How do C Arrays work?</h3>
<p><strong>Definition:</strong> <strong>Contiguous memory block</strong> with elements of same type. <strong>Fixed size</strong>.</p>

<table>
  <thead>
    <tr>
      <th>Operation</th>
      <th>Time</th>
      <th>Notes</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Access by index</td>
      <td>O(1)</td>
      <td>Formula: base + index * size</td>
    </tr>
    <tr>
      <td>Search</td>
      <td>O(n)</td>
      <td>Linear</td>
    </tr>
    <tr>
      <td>Sort</td>
      <td>O(n log n)</td>
      <td>Requires algorithm</td>
    </tr>
  </tbody>
</table>

<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="rouge-code"><pre><span class="kt">int</span> <span class="n">arr</span><span class="p">[</span><span class="mi">5</span><span class="p">];</span>
<span class="kt">int</span> <span class="n">arr</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">};</span>
<span class="kt">int</span> <span class="n">arr</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">};</span>

<span class="n">arr</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">10</span><span class="p">;</span>          <span class="c1">// O(1)</span>
<span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="n">arr</span><span class="p">[</span><span class="mi">4</span><span class="p">];</span>       <span class="c1">// O(1)</span>

<span class="kt">int</span> <span class="n">matrix</span><span class="p">[</span><span class="mi">2</span><span class="p">][</span><span class="mi">3</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">},</span> <span class="p">{</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">}</span> <span class="p">};</span>

<span class="kt">void</span> <span class="nf">func</span><span class="p">(</span><span class="kt">int</span> <span class="o">*</span><span class="n">arr</span><span class="p">,</span> <span class="kt">int</span> <span class="n">size</span><span class="p">)</span> <span class="p">{}</span>
<span class="cp">#define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p><strong>Interview Questions:</strong></p>

<p><strong>Q: Contiguous memory enables?</strong><br />
A: O(1) random access, good cache locality.</p>

<p><strong>Q: Array decay?</strong><br />
A: Array decays to pointer when passed, loses size info.</p>

<hr />

<h3 id="how-are-strings-implemented-in-c"><a name="string-c"></a>How are Strings implemented in C?</h3>
<p><strong>Definition:</strong> <strong>Null-terminated</strong> character array.</p>

<table>
  <thead>
    <tr>
      <th>Operation</th>
      <th>Time</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Access char</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>strlen</td>
      <td>O(n)</td>
    </tr>
    <tr>
      <td>strcpy</td>
      <td>O(n)</td>
    </tr>
    <tr>
      <td>strcat</td>
      <td>O(n+m)</td>
    </tr>
    <tr>
      <td>strcmp</td>
      <td>O(n)</td>
    </tr>
    <tr>
      <td>strchr</td>
      <td>O(n)</td>
    </tr>
    <tr>
      <td>strstr</td>
      <td>O(n*m)</td>
    </tr>
  </tbody>
</table>

<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
</pre></td><td class="rouge-code"><pre><span class="cp">#include</span> <span class="cpf">&lt;string.h&gt;</span><span class="cp">
</span>
<span class="kt">char</span> <span class="n">s</span><span class="p">[]</span> <span class="o">=</span> <span class="s">"hello"</span><span class="p">;</span>
<span class="kt">char</span> <span class="o">*</span><span class="n">s</span> <span class="o">=</span> <span class="s">"hello"</span><span class="p">;</span>

<span class="n">strlen</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>                  <span class="c1">// O(n)</span>
<span class="n">strcpy</span><span class="p">(</span><span class="n">dest</span><span class="p">,</span> <span class="n">src</span><span class="p">)</span>         <span class="c1">// O(n)</span>
<span class="n">strcat</span><span class="p">(</span><span class="n">dest</span><span class="p">,</span> <span class="n">src</span><span class="p">)</span>         <span class="c1">// O(n+m)</span>
<span class="n">strcmp</span><span class="p">(</span><span class="n">s1</span><span class="p">,</span> <span class="n">s2</span><span class="p">)</span>            <span class="c1">// O(n)</span>
<span class="n">strncpy</span><span class="p">(</span><span class="n">dest</span><span class="p">,</span> <span class="n">src</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>     <span class="c1">// O(n)</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p><strong>Interview Questions:</strong></p>

<p><strong>Q: Buffer overflow?</strong><br />
A: Writing beyond array bounds, security vulnerability.</p>

<p><strong>Q: Safe copy?</strong><br />
A: Use strncpy with null termination, or snprintf.</p>

<hr />

<h3 id="how-do-you-define-a-struct"><a name="struct"></a>How do you define a Struct?</h3>
<p><strong>Definition:</strong> <strong>Composite type</strong> grouping different types.</p>

<table>
  <thead>
    <tr>
      <th>Operation</th>
      <th>Time</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Access member</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>Copy</td>
      <td>O(n)</td>
    </tr>
    <tr>
      <td>Size</td>
      <td>O(1)</td>
    </tr>
  </tbody>
</table>

<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="rouge-code"><pre><span class="k">struct</span> <span class="n">Point</span> <span class="p">{</span>
    <span class="kt">int</span> <span class="n">x</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">y</span><span class="p">;</span>
<span class="p">};</span>

<span class="k">struct</span> <span class="n">Point</span> <span class="n">p1</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">};</span>
<span class="n">p1</span><span class="p">.</span><span class="n">x</span> <span class="o">=</span> <span class="mi">10</span><span class="p">;</span>                 <span class="c1">// O(1)</span>

<span class="k">typedef</span> <span class="k">struct</span> <span class="p">{</span>
    <span class="kt">int</span> <span class="n">x</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">y</span><span class="p">;</span>
<span class="p">}</span> <span class="n">Point</span><span class="p">;</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p><strong>Interview Questions:</strong></p>

<p><strong>Q: Padding?</strong><br />
A: Compiler adds padding for alignment. Size may be larger.</p>

<hr />

<h3 id="what-is-the-purpose-of-a-union"><a name="union"></a>What is the purpose of a Union?</h3>
<p><strong>Definition:</strong> All members share <strong>same memory location</strong>.</p>

<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
</pre></td><td class="rouge-code"><pre><span class="k">union</span> <span class="n">Data</span> <span class="p">{</span>
    <span class="kt">int</span> <span class="n">i</span><span class="p">;</span>
    <span class="kt">float</span> <span class="n">f</span><span class="p">;</span>
    <span class="kt">char</span> <span class="n">c</span><span class="p">;</span>
<span class="p">};</span>

<span class="k">union</span> <span class="n">Data</span> <span class="n">d</span><span class="p">;</span>
<span class="n">d</span><span class="p">.</span><span class="n">i</span> <span class="o">=</span> <span class="mi">10</span><span class="p">;</span>                  <span class="c1">// O(1)</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<hr />

<h3 id="how-to-use-an-enum"><a name="enum"></a>How to use an Enum?</h3>
<p><strong>Definition:</strong> <strong>Named integer constants</strong>.</p>

<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre><span class="k">enum</span> <span class="n">Day</span> <span class="p">{</span><span class="n">SUN</span><span class="p">,</span> <span class="n">MON</span><span class="p">,</span> <span class="n">TUE</span><span class="p">};</span>
<span class="k">enum</span> <span class="n">Color</span> <span class="p">{</span><span class="n">RED</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="n">GREEN</span> <span class="o">=</span> <span class="mi">2</span><span class="p">};</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<hr />

<h3 id="how-do-pointers-work-in-c"><a name="pointer"></a>How do Pointers work in C?</h3>
<p><strong>Definition:</strong> Variable storing a <strong>memory address</strong>.</p>

<table>
  <thead>
    <tr>
      <th>Operation</th>
      <th>Time</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Dereference</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>Address-of</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>Arithmetic</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>malloc</td>
      <td>O(1)*</td>
    </tr>
    <tr>
      <td>free</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>realloc</td>
      <td>O(n)*</td>
    </tr>
  </tbody>
</table>

<p>*Amortized</p>

<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
</pre></td><td class="rouge-code"><pre><span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">10</span><span class="p">;</span>
<span class="kt">int</span> <span class="o">*</span><span class="n">p</span> <span class="o">=</span> <span class="o">&amp;</span><span class="n">x</span><span class="p">;</span>              <span class="c1">// O(1)</span>
<span class="o">*</span><span class="n">p</span> <span class="o">=</span> <span class="mi">20</span><span class="p">;</span>                  <span class="c1">// O(1)</span>

<span class="kt">int</span> <span class="o">*</span><span class="n">arr</span> <span class="o">=</span> <span class="n">malloc</span><span class="p">(</span><span class="n">n</span> <span class="o">*</span> <span class="nf">sizeof</span><span class="p">(</span><span class="kt">int</span><span class="p">));</span>  <span class="c1">// O(1)*</span>
<span class="n">free</span><span class="p">(</span><span class="n">arr</span><span class="p">);</span>                 <span class="c1">// O(1)</span>

<span class="kt">void</span> <span class="nf">alloc</span><span class="p">(</span><span class="kt">int</span> <span class="o">**</span><span class="n">p</span><span class="p">)</span> <span class="p">{</span>
    <span class="o">*</span><span class="n">p</span> <span class="o">=</span> <span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="kt">int</span><span class="p">));</span>  <span class="c1">// O(1)*</span>
<span class="p">}</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p><strong>Interview Questions:</strong></p>

<p><strong>Q: malloc vs calloc?</strong><br />
A: malloc: uninitialized. calloc: zero-initialized.</p>

<p><strong>Q: Memory leak?</strong><br />
A: Memory allocated but never freed.</p>

<hr />

<h3 id="how-do-you-manually-implement-a-linked-list"><a name="linked-list"></a>How do you manually implement a Linked List?</h3>

<table>
  <thead>
    <tr>
      <th>Operation</th>
      <th>Time</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Insert at head</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>Insert at tail</td>
      <td>O(1)*</td>
    </tr>
    <tr>
      <td>Delete at head</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>Search</td>
      <td>O(n)</td>
    </tr>
    <tr>
      <td>Access by index</td>
      <td>O(n)</td>
    </tr>
  </tbody>
</table>

<p>*If tail pointer maintained</p>

<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="rouge-code"><pre><span class="k">struct</span> <span class="n">Node</span> <span class="p">{</span>
    <span class="kt">int</span> <span class="n">data</span><span class="p">;</span>
    <span class="k">struct</span> <span class="n">Node</span> <span class="o">*</span><span class="n">next</span><span class="p">;</span>
<span class="p">};</span>

<span class="k">struct</span> <span class="n">Node</span> <span class="o">*</span><span class="n">new</span> <span class="o">=</span> <span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">Node</span><span class="p">));</span>
<span class="n">new</span><span class="o">-&gt;</span><span class="n">data</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="n">new</span><span class="o">-&gt;</span><span class="n">next</span> <span class="o">=</span> <span class="n">head</span><span class="p">;</span>
<span class="n">head</span> <span class="o">=</span> <span class="n">new</span><span class="p">;</span>

<span class="k">for</span><span class="p">(</span><span class="k">struct</span> <span class="n">Node</span> <span class="o">*</span><span class="n">p</span> <span class="o">=</span> <span class="n">head</span><span class="p">;</span> <span class="n">p</span><span class="p">;</span> <span class="n">p</span> <span class="o">=</span> <span class="n">p</span><span class="o">-&gt;</span><span class="n">next</span><span class="p">)</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">"%d "</span><span class="p">,</span> <span class="n">p</span><span class="o">-&gt;</span><span class="n">data</span><span class="p">);</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<hr />

<h2 id="where-can-i-find-a-quick-reference">Where can I find a Quick Reference?</h2>

<table>
  <thead>
    <tr>
      <th>Need</th>
      <th>Python</th>
      <th>C++</th>
      <th>C</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Sequence, index</td>
      <td>list</td>
      <td>vector</td>
      <td>array</td>
    </tr>
    <tr>
      <td>Both ends</td>
      <td>deque</td>
      <td>deque</td>
      <td>manual</td>
    </tr>
    <tr>
      <td>Unique</td>
      <td>set</td>
      <td>unordered_set</td>
      <td>-</td>
    </tr>
    <tr>
      <td>Key-value</td>
      <td>dict</td>
      <td>unordered_map</td>
      <td>-</td>
    </tr>
    <tr>
      <td>Sorted</td>
      <td>set</td>
      <td>set</td>
      <td>-</td>
    </tr>
    <tr>
      <td>Stack</td>
      <td>list/stack</td>
      <td>stack</td>
      <td>manual</td>
    </tr>
    <tr>
      <td>Queue</td>
      <td>deque</td>
      <td>queue</td>
      <td>manual</td>
    </tr>
    <tr>
      <td>Priority</td>
      <td>heapq</td>
      <td>priority_queue</td>
      <td>manual</td>
    </tr>
  </tbody>
</table>

<hr />

<h2 id="what-is-the-complexity-summary">What is the Complexity Summary?</h2>

<h3 id="python-1">Python</h3>

<table>
  <thead>
    <tr>
      <th>Structure</th>
      <th>Access</th>
      <th>Search</th>
      <th>Insert</th>
      <th>Delete</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>list</td>
      <td>O(1)</td>
      <td>O(n)</td>
      <td>O(1)*</td>
      <td>O(n)</td>
    </tr>
    <tr>
      <td>tuple</td>
      <td>O(1)</td>
      <td>O(n)</td>
      <td>N/A</td>
      <td>N/A</td>
    </tr>
    <tr>
      <td>set</td>
      <td>-</td>
      <td>O(1)*</td>
      <td>O(1)*</td>
      <td>O(1)*</td>
    </tr>
    <tr>
      <td>dict</td>
      <td>-</td>
      <td>O(1)*</td>
      <td>O(1)*</td>
      <td>O(1)*</td>
    </tr>
  </tbody>
</table>

<p>*Average **Worst case</p>

<h3 id="c-stl-1">C++ STL</h3>

<table>
  <thead>
    <tr>
      <th>Container</th>
      <th>Access</th>
      <th>Search</th>
      <th>Insert</th>
      <th>Delete</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>vector</td>
      <td>O(1)</td>
      <td>O(n)</td>
      <td>O(1)*</td>
      <td>O(n)</td>
    </tr>
    <tr>
      <td>deque</td>
      <td>O(1)</td>
      <td>O(n)</td>
      <td>O(1)</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>list</td>
      <td>O(n)</td>
      <td>O(n)</td>
      <td>O(1)</td>
      <td>O(1)</td>
    </tr>
    <tr>
      <td>set/map</td>
      <td>-</td>
      <td>O(log n)</td>
      <td>O(log n)</td>
      <td>O(log n)</td>
    </tr>
    <tr>
      <td>unordered_set/map</td>
      <td>-</td>
      <td>O(1)*</td>
      <td>O(1)*</td>
      <td>O(1)*</td>
    </tr>
  </tbody>
</table>

<p>*Average **Worst case</p>

<h3 id="c">C</h3>

<table>
  <thead>
    <tr>
      <th>Structure</th>
      <th>Access</th>
      <th>Search</th>
      <th>Notes</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>array</td>
      <td>O(1)</td>
      <td>O(n)</td>
      <td>Fixed size</td>
    </tr>
    <tr>
      <td>linked list</td>
      <td>O(n)</td>
      <td>O(n)</td>
      <td>Dynamic</td>
    </tr>
  </tbody>
</table>]]></content><author><name>Email me at</name><email>gls.prasadraju@gmail.com</email></author><category term="Python" /><category term="C++" /><category term="C" /><category term="Data Structures" /><category term="Algorithms" /><summary type="html"><![CDATA[A comprehensive guide and quick reference for in-built data structures in Python, C++, and C. Discover time complexities, common operations, and interview questions.]]></summary></entry><entry><title type="html">Sorting</title><link href="https://iamprasadraju.github.io/notes/2025-12-08-sorting.html" rel="alternate" type="text/html" title="Sorting" /><published>2025-12-08T00:00:00+00:00</published><updated>2025-12-08T00:00:00+00:00</updated><id>https://iamprasadraju.github.io/notes/sorting</id><content type="html" xml:base="https://iamprasadraju.github.io/notes/2025-12-08-sorting.html"><![CDATA[<p>A <strong>Sorting Algorithm</strong> is an algorithm that puts elements of a list into an order.
The most frequently used orders are <strong>numerical order</strong> and <strong>lexicographical order</strong>, and either <strong>ascending</strong> or <strong>descending</strong>.</p>

<p>Efficient sorting is important for optimizing the efficiency of other algorithms (such as search and merge algorithms) that require input data to be in sorted lists. Sorting is also often useful for <strong>canonicalizing data</strong> and for producing <strong>human-readable output</strong>.</p>

<p><strong>Sorting Algorithm</strong></p>

<ol>
  <li><a href="#what-is-selection-sort">Selection Sort</a></li>
  <li><a href="#what-is-insertion-sort">Insertion Sort</a></li>
  <li><a href="#what-is-bubble-sort">Bubble Sort</a></li>
  <li><a href="#quick-sort">Quick Sort</a></li>
  <li><a href="#merge-sort">Merge Sort</a></li>
  <li><a href="#heap-sort">Heap sort</a></li>
</ol>

<h2 id="what-is-selection-sort">What is Selection Sort?</h2>

<hr />

<p><strong>Selection Sort</strong> is a simple, in-place, comparison-based sorting algorithm. It works by repeatedly finding the <strong>minimum</strong> (or maximum) element from the unsorted portion of the list and placing it at the beginning (or end) of the sorted portion.</p>

<p><img src="https://upload.wikimedia.org/wikipedia/commons/9/94/Selection-Sort-Animation.gif" style="transform: rotate(270deg) scale(1.3); display:block; margin:0 auto;" /></p>

<h4 id="how-does-it-work">How does it work?</h4>

<ol>
  <li>
    <p><strong>Selection</strong>: The algorithm scans the unsorted part of the array to find the smallest (or largest, for descending) element.</p>
  </li>
  <li>
    <p><strong>Swapping</strong>: It swaps this smallest element with the first element of the unsorted part.</p>
  </li>
  <li>
    <p><strong>Expansion</strong>: The boundary of the sorted portion moves one step to the right, and the process repeats for the remaining unsorted elements.</p>
  </li>
  <li>
    <p><strong>Completion</strong>: This continues until the entire array is sorted, typically requiring n-1 passes for an array of size n.</p>
  </li>
</ol>

<h4 id="what-is-its-time-and-space-complexity">What is its time and space complexity?</h4>
<ul>
  <li>
    <p><strong>Time Complexity</strong>: $O(n^2)$ for best, average, and worst-case scenarios. Because the algorithm must scan the remaining unsorted part entirely to find the minimum in each pass, it is not adaptive to already sorted data.</p>
  </li>
  <li>
    <p><strong>Space Complexity</strong>: $O(1)$ , as it only requires a constant amount of extra memory for temporary variables used during swapping</p>
  </li>
</ul>

<h4 id="what-are-its-key-characteristics">What are its key characteristics?</h4>
<ul>
  <li><strong>In-Place</strong>: It modifies the original array directly without needing extra storage.</li>
  <li><strong>Unstable</strong>: By default, it may change the relative order of equal elements because it swaps non-adjacent elements.</li>
  <li><strong>Minimal Swaps</strong>: While it has poor time complexity, it performs only $O(n)$ swaps, making it useful for memory environments where write operations are costly (e.g., Flash memory or EEPROM).</li>
</ul>

<h4 id="when-should-you-use-it">When should you use it?</h4>
<ul>
  <li><strong>Small Data Sets</strong>: Where simplicity is preferred over peak efficiency.</li>
  <li><strong>Costly Memory Writes</strong>: When minimizing the number of write operations is more important than reducing comparisons.</li>
  <li><strong>Educational Purposes</strong>: Its straightforward logic makes it an ideal introductory algorithm for learning sorting fundamentals.</li>
</ul>

<blockquote>
  <p><strong>Class:</strong> Sorting algorithm<br />
<strong>Data structure:</strong> Array</p>

  <p><strong>Worst-case performance:</strong></p>
  <ul>
    <li><code class="language-plaintext highlighter-rouge">O(n²)</code> comparisons</li>
    <li><code class="language-plaintext highlighter-rouge">O(n)</code> swaps</li>
  </ul>

  <p><strong>Best-case performance:</strong></p>
  <ul>
    <li><code class="language-plaintext highlighter-rouge">O(n²)</code> comparisons</li>
    <li><code class="language-plaintext highlighter-rouge">O(1)</code> swap</li>
  </ul>

  <p><strong>Average performance:</strong></p>
  <ul>
    <li><code class="language-plaintext highlighter-rouge">O(n²)</code> comparisons</li>
    <li><code class="language-plaintext highlighter-rouge">O(n)</code> swaps</li>
  </ul>

  <p><strong>Worst-case space complexity:</strong></p>
  <ul>
    <li><code class="language-plaintext highlighter-rouge">O(1)</code> auxiliary</li>
  </ul>

  <p><strong>Optimal:</strong> No</p>
</blockquote>

<h2 id="what-is-bubble-sort">What is Bubble Sort?</h2>

<hr />

<p><strong>Bubble Sort</strong> is a simple, comparison-based algorithm where each pair of adjacent elements is compared and swapped if they are in the wrong order. This process repeats until the largest elements “bubble up” to their correct positions at the end of the list.</p>

<h4 id="how-does-it-work-1">How does it work?</h4>

<ol>
  <li><strong>Compare Adjacent Elements</strong>: Start at the beginning of the list and compare the first two elements.</li>
  <li><strong>Swap</strong>: If the first element is larger than the second, swap them.</li>
  <li><strong>Move Forward</strong>: Move to the next pair and repeat the comparison/swap until the end of the list is reached.</li>
  <li><strong>Complete the Pass</strong>: After the first pass, the largest element is guaranteed to be at the final position.</li>
  <li><strong>Repeat</strong>: Repeat the entire process for the remaining unsorted elements until a full pass occurs without any swaps.</li>
</ol>

<h4 id="what-is-its-time-and-space-complexity-1">What is its time and space complexity?</h4>

<ul>
  <li><strong>Best Case</strong>: $O(n)$ – Occurs when the array is already sorted (requires an optimized version that stops if no swaps are made).</li>
  <li><strong>Average Case</strong>: $O(n^2)$ – Occurs when elements are in random order.</li>
  <li><strong>Worst Case</strong>: $O(n^2)$ – Occurs when the array is sorted in reverse order.</li>
  <li><strong>Space Complexity</strong>: $O(1)$ – It is an in-place algorithm, requiring no extra storage.</li>
</ul>

<h4 id="what-are-its-key-characteristics-1">What are its key characteristics?</h4>

<ul>
  <li><strong>Stable</strong>: It maintains the relative order of elements with equal values.</li>
  <li><strong>In-Place</strong>: It only requires a constant amount of additional memory space.</li>
  <li><strong>Simple</strong>: It is one of the easiest algorithms to understand and write, making it a popular teaching tool.</li>
  <li><strong>High Overhead</strong>: It performs a large number of swaps compared to other algorithms like Insertion Sort.</li>
</ul>

<h4 id="when-should-you-use-it-1">When should you use it?</h4>

<ul>
  <li><strong>Educational Purposes</strong>: Ideal for introducing the concept of sorting and algorithmic logic to beginners.</li>
  <li><strong>Very Small Datasets</strong>: Can be used when the dataset size is so small that complexity doesn’t impact performance.</li>
  <li><strong>Nearly Sorted Data</strong>: Useful if you only need to swap one or two elements that are slightly out of place.</li>
  <li><strong>Detecting a Sorted List</strong>: The optimized version is very efficient at simply confirming if a list is already sorted.</li>
</ul>

<p>Do you want to see how Selection Sort compares to these two?</p>

<center>
  <img src="https://upload.wikimedia.org/wikipedia/commons/c/c8/Bubble-sort-example-300px.gif" />
</center>

<h3 id="how-does-a-step-by-step-example-look">How does a step-by-step example look?</h3>

<div class="language-text highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="rouge-code"><pre>Take an array of numbers "5 1 4 2 8"

First pass
( 5 1 4 2 8 ) → ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 &gt; 1.
( 1 5 4 2 8 ) → ( 1 4 5 2 8 ), Swap since 5 &gt; 4
( 1 4 5 2 8 ) → ( 1 4 2 5 8 ), Swap since 5 &gt; 2
( 1 4 2 5 8 ) → ( 1 4 2 5 8 ), Now, since these elements are already in order (8 &gt; 5), algorithm does not swap them.

Second pass
( 1 4 2 5 8 ) → ( 1 4 2 5 8 )
( 1 4 2 5 8 ) → ( 1 2 4 5 8 ), Swap since 4 &gt; 2
( 1 2 4 5 8 ) → ( 1 2 4 5 8 )
( 1 2 4 5 8 ) → ( 1 2 4 5 8 )

Third pass
( 1 2 4 5 8 ) → ( 1 2 4 5 8 )
( 1 2 4 5 8 ) → ( 1 2 4 5 8 )
( 1 2 4 5 8 ) → ( 1 2 4 5 8 )
( 1 2 4 5 8 ) → ( 1 2 4 5 8 )
</pre></td></tr></tbody></table></code></pre></div></div>
<p> </p>

<blockquote>
  <p><strong>Class:</strong> Sorting algorithm<br />
<strong>Data structure:</strong> Array</p>

  <p><strong>Worst-case performance:</strong></p>
  <ul>
    <li><code class="language-plaintext highlighter-rouge">O(n²)</code> comparisons</li>
    <li><code class="language-plaintext highlighter-rouge">O(n²)</code> swaps</li>
  </ul>

  <p><strong>Best-case performance:</strong></p>
  <ul>
    <li><code class="language-plaintext highlighter-rouge">O(n)</code> comparisons</li>
    <li><code class="language-plaintext highlighter-rouge">O(1)</code> swaps</li>
  </ul>

  <p><strong>Average performance:</strong></p>
  <ul>
    <li><code class="language-plaintext highlighter-rouge">O(n²)</code> comparisons</li>
    <li><code class="language-plaintext highlighter-rouge">O(n²)</code> swaps</li>
  </ul>

  <p><strong>Worst-case space complexity:</strong></p>
  <ul>
    <li><code class="language-plaintext highlighter-rouge">O(n)</code> total</li>
    <li><code class="language-plaintext highlighter-rouge">O(1)</code> auxiliary</li>
  </ul>

  <p><strong>Optimal:</strong> No</p>
</blockquote>

<h2 id="what-is-insertion-sort">What is Insertion Sort?</h2>

<hr />

<p><strong>Insertion sort</strong> is a simple, intuitive sorting algorithm that builds a final sorted list one element at a time. It is often compared to the way a person might sort a hand of playing cards: you pick one card at a time and insert it into its correct position relative to the cards you are already holding.</p>
<center>
  <img src="https://upload.wikimedia.org/wikipedia/commons/0/0f/Insertion-sort-example-300px.gif" />
  
<div>
  <img src="https://upload.wikimedia.org/wikipedia/commons/3/32/Insertionsort-before.png" />
</div>
  
  <p>Becomes</p>
  
  <img src="https://upload.wikimedia.org/wikipedia/commons/d/d9/Insertionsort-after.png" />
  </center>

<h4 id="how-does-it-work-2">How does it work?</h4>
<p>The algorithm virtually splits the input array into two parts: a sorted portion (on the left) and an unsorted portion (on the right).</p>

<ol>
  <li><strong>Initialize</strong>: Assume the first element is already sorted.</li>
  <li><strong>Pick a Key</strong>: Move to the next element (the “key”) from the unsorted part.</li>
  <li><strong>Compare and Shift</strong>: Compare this key with the elements in the sorted portion from right to left. If a sorted element is larger than the key, shift it one position to the right.</li>
  <li><strong>Insert</strong>: Once you find the correct spot-where the element to the left is smaller than or equal to the key-insert the key into that position.</li>
  <li><strong>Repeat</strong>: Continue this process until every element from the unsorted part has been moved to its correct position.</li>
</ol>

<h4 id="what-is-its-time-and-space-complexity-2">What is its time and space complexity?</h4>
<ul>
  <li><strong>Best Case</strong>: $O(n)$ – Occurs when the array is already sorted; the algorithm makes one pass with no shifts needed.</li>
  <li><strong>Average Case</strong>: $O(n^2)$ – Occurs with random data where elements must be shifted through roughly half of the sorted portion.</li>
  <li><strong>Worst Case</strong>: $O(n^2)$ – Occurs when the array is in reverse order, requiring every element to be compared and shifted across the entire sorted section.</li>
  <li><strong>Space Complexity</strong>: $O(1)$ – It is an in-place algorithm, meaning it requires only a constant amount of extra memory regardless of the dataset size.</li>
</ul>

<h4 id="what-are-its-key-characteristics-2">What are its key characteristics?</h4>
<ul>
  <li><strong>Stable</strong>: It maintains the relative order of duplicate elements.</li>
  <li><strong>Adaptive</strong>: Performance improves significantly if the input is already partially or nearly sorted.</li>
  <li><strong>Online</strong>: It can sort data as it is received, one item at a time, without needing the full dataset upfront.</li>
  <li><strong>Low Overhead</strong>: Because of its simplicity, it has very low overhead compared to more complex algorithms.</li>
</ul>

<h4 id="when-should-you-use-it-2">When should you use it?</h4>
<ul>
  <li><strong>Small Datasets</strong>: It is often faster than advanced algorithms like QuickSort or MergeSort for small lists (typically 10–50 elements).</li>
  <li><strong>Nearly Sorted Data</strong>: It is the preferred choice for data that is already mostly in order.</li>
  <li><strong>Memory-Constrained Environments</strong>: Ideal for embedded systems where memory is limited because it uses no extra space.</li>
  <li><strong>Hybrid Subroutines</strong>: It is frequently used as the final step in high-performance algorithms like Timsort or Introsort once sub-arrays become small enough.</li>
</ul>

<p>Example:</p>

<div class="language-text highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
</pre></td><td class="rouge-code"><pre>3  7  4  9  5  2  6  1
3* 7  4  9  5  2  6  1
3  7* 4  9  5  2  6  1
3  4* 7  9  5  2  6  1
3  4  7  9* 5  2  6  1
3  4  5* 7  9  2  6  1
2* 3  4  5  7  9  6  1
2  3  4  5  6* 7  9  1
1* 2  3  4  5  6  7  9
</pre></td></tr></tbody></table></code></pre></div></div>
<p><br /></p>

<blockquote>
  <p><strong>Algorithm:</strong> <strong>Insertion Sort</strong></p>

  <p><strong>Class:</strong> Sorting algorithm<br />
<strong>Data structure:</strong> Array</p>

  <p><strong>Worst-case performance:</strong></p>
  <ul>
    <li><code class="language-plaintext highlighter-rouge">O(n²)</code> comparisons</li>
    <li><code class="language-plaintext highlighter-rouge">O(n²)</code> swaps</li>
  </ul>

  <p><strong>Best-case performance:</strong></p>
  <ul>
    <li><code class="language-plaintext highlighter-rouge">O(n)</code> comparisons</li>
    <li><code class="language-plaintext highlighter-rouge">O(1)</code> swaps</li>
  </ul>

  <p><strong>Average performance:</strong></p>
  <ul>
    <li><code class="language-plaintext highlighter-rouge">O(n²)</code> comparisons</li>
    <li><code class="language-plaintext highlighter-rouge">O(n²)</code> swaps</li>
  </ul>

  <p><strong>Worst-case space complexity:</strong></p>
  <ul>
    <li><code class="language-plaintext highlighter-rouge">O(n)</code> total</li>
    <li><code class="language-plaintext highlighter-rouge">O(1)</code> auxiliary</li>
  </ul>

  <p><strong>Optimal:</strong> No</p>
</blockquote>

<p> </p>

<table>
  <thead>
    <tr>
      <th><strong>Algorithm</strong></th>
      <th><strong>Time Complexity (Best)</strong></th>
      <th><strong>Time Complexity (Average)</strong></th>
      <th><strong>Time Complexity (Worst)</strong></th>
      <th><strong>Space Complexity</strong></th>
      <th><strong>Stable</strong></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><strong>Bubble Sort</strong></td>
      <td>O(n)</td>
      <td>O(n²)</td>
      <td>O(n²)</td>
      <td>O(1)</td>
      <td>Yes</td>
    </tr>
    <tr>
      <td><strong>Selection Sort</strong></td>
      <td>O(n²)</td>
      <td>O(n²)</td>
      <td>O(n²)</td>
      <td>O(1)</td>
      <td>No</td>
    </tr>
    <tr>
      <td><strong>Insertion Sort</strong></td>
      <td>O(n)</td>
      <td>O(n²)</td>
      <td>O(n²)</td>
      <td>O(1)</td>
      <td>Yes</td>
    </tr>
    <tr>
      <td><strong>Merge Sort</strong></td>
      <td>O(n log n)</td>
      <td>O(n log n)</td>
      <td>O(n log n)</td>
      <td>O(n)</td>
      <td>Yes</td>
    </tr>
    <tr>
      <td><strong>Quick Sort</strong></td>
      <td>O(n log n)</td>
      <td>O(n log n)</td>
      <td>O(n²)</td>
      <td>O(log n)</td>
      <td>No</td>
    </tr>
    <tr>
      <td><strong>Heap Sort</strong></td>
      <td>O(n log n)</td>
      <td>O(n log n)</td>
      <td>O(n log n)</td>
      <td>O(1)</td>
      <td>No</td>
    </tr>
  </tbody>
</table>]]></content><author><name>Email me at</name><email>gls.prasadraju@gmail.com</email></author><category term="Algorithms" /><category term="Computer Science" /><category term="Sorting" /><category term="Data Structures" /><summary type="html"><![CDATA[A comprehensive guide to sorting algorithms, including Selection Sort, Bubble Sort, and Insertion Sort, covering their mechanics, complexities, and use cases.]]></summary></entry><entry><title type="html">Working With ESP32</title><link href="https://iamprasadraju.github.io/notes/2025-08-21-working-with-esp32.html" rel="alternate" type="text/html" title="Working With ESP32" /><published>2025-08-21T00:00:00+00:00</published><updated>2025-08-21T00:00:00+00:00</updated><id>https://iamprasadraju.github.io/notes/working-with-esp32</id><content type="html" xml:base="https://iamprasadraju.github.io/notes/2025-08-21-working-with-esp32.html"><![CDATA[<p><strong>ESP32</strong> is a family of low-cost, energy-efficient microcontrollers that integrate both Wi-Fi and Bluetooth capabilities. These chips feature a variety of processing options, including the <strong>Tensilica Xtensa LX6 microprocessor</strong> available in both dual-core and single-core variants, the <strong>Xtensa LX7 dual-core processor</strong>, or a single-core <strong>RISC-V microprocessor</strong>. In addition, the ESP32 incorporates components essential for wireless data communication such as built-in antenna switches, an RF balun, power amplifiers, low-noise receivers, filters, and power-management modules.</p>

<p><img src="https://docs.espressif.com/projects/esp-dev-kits/en/latest/esp32/_images/esp32-devkitc-v4-functional-overview.png" alt="ESP32 Overview" /></p>

<h3 id="what-are-the-esp32-hardware-specifications">What are the ESP32 Hardware Specifications?</h3>

<ol>
  <li><strong>Processor</strong>: CPU: Xtensa dual-core (or single-core) 32-bit LX6, Operating on 160 - 240 MHz</li>
  <li><strong>Memory</strong>: 520kb RAM, 448kb ROM</li>
  <li><strong>Wireless connectivity</strong>: Wi-Fi: 802.11 b/g/n, Bluetooth: v4.2 BR/EDR and BLE (shares the radio with Wi-Fi)</li>
</ol>

<hr />

<h3 id="which-softwares-and-tools-are-used-for-esp32">Which Softwares and Tools are Used for ESP32?</h3>

<p><strong>USB-to-UART Drivers:</strong> To Establish Serial Connection with ESP32 ➡️ <a href="https://www.silabs.com/software-and-tools/usb-to-uart-bridge-vcp-drivers">Download Drivers</a></p>

<p><strong>ESP-IDF (SDK)</strong>: For C/C++ builds. (Low-level code —–&gt; ESP-IDF —–&gt; Machine Code). It comes with cross-compiler toolchain, Debugger, Linker etc.
➡️ <a href="https://github.com/espressif/esp-idf"><strong>Github</strong></a>, <a href="https://docs.espressif.com/projects/esp-idf/en/stable/esp32/get-started/index.html"><strong>Get Started (ESP-IDF)</strong></a></p>

<p><strong>esptool</strong> : A Python-based, open-source, platform-independent serial utility for flashing, provisioning, and interacting with Espressif SoCs. <a href="https://docs.espressif.com/projects/esptool/en/latest/esp32/esptool/index.html"><strong>Documentation</strong></a></p>

<p><strong>Installation</strong> -</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre>pip <span class="nb">install </span>esptool
</pre></td></tr></tbody></table></code></pre></div></div>

<p>Useful commands are: <code class="language-plaintext highlighter-rouge">esptool erase_flash</code> -&gt; Erase all flash memory</p>

<hr />

<h1 id="how-to-use-micropython-with-esp32">How to Use MicroPython with ESP32?</h1>

<p><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/MicroPython_new_logo.svg/1920px-MicroPython_new_logo.svg.png" width="10%" /></p>

<p><strong>MicroPython</strong> is a software implementation of a programming language largely compatible with Python 3, written in C, that is optimized to run on a microcontroller.</p>

<p>MicroPython consists of a Python compiler to bytecode and a runtime interpreter of that bytecode. The user is presented with an interactive prompt (the <strong>REPL</strong>) to execute supported commands immediately. Included are a selection of core Python libraries; MicroPython includes modules which give the programmer access to low-level hardware.</p>

<p>MicroPython does have an <strong>inline assembler</strong>, which lets the code run at full speed, but it is not portable across different microcontrollers.</p>

<p><a href="https://micropython.org/download/">Download MicroPython Firmware</a></p>

<p><strong>Flashing MicroPython Firmware</strong> - <code class="language-plaintext highlighter-rouge">esptool.py --baud 460800 write_flash 0x1000 ESP32_BOARD_NAME-DATE-VERSION.bin</code></p>

<p><strong>Install Firmware – USE REPL (to interact and run micropython) – Build Projects</strong></p>

<p>➡️ <a href="https://docs.micropython.org/en/latest/index.html"><strong>MicroPython Docs</strong></a></p>

<h3 id="what-tools-are-available-for-micropython">What Tools are Available for MicroPython?</h3>

<p><strong>mpremote</strong> - The mpremote command line tool provides an integrated set of utilities to remotely interact with, manage the filesystem on, and automate a MicroPython device over a serial connection.</p>

<ol>
  <li><strong>connect</strong> - connect to specified device via name: <code class="language-plaintext highlighter-rouge">$ mpremote connect &lt;device&gt;</code></li>
  <li><strong>repl</strong> - enter the REPL on the connected device: <code class="language-plaintext highlighter-rouge">$ mpremote repl [--options]</code></li>
</ol>

<p>For more commands visit <a href="https://docs.micropython.org/en/latest/reference/mpremote.html">Manual</a></p>

<p><strong>WebREPL</strong> -
WebREPL allows you to use the Python prompt over WiFi, connecting through a browser. The latest versions of Firefox and Chrome are supported.</p>

<ol>
  <li>Single connection/channel, multiplexing terminal access, filesystem access, and board control.</li>
  <li>Network ready and Web technologies ready (allowing access directly from a browser with an HTML-based client).</li>
</ol>

<p><strong>Setup</strong> -</p>
<ol>
  <li>Install MicroPython Firmware</li>
  <li>Connect device and PC to same Wifi network</li>
  <li>Enter REPL mode</li>
  <li>Run - <code class="language-plaintext highlighter-rouge">import webrepl_setup</code></li>
  <li>Enable WebREPL mode, it will show WebREPL server started on <code class="language-plaintext highlighter-rouge">http://192.168.31.169:8266/</code></li>
</ol>

<p><img src="https://i0.wp.com/blog.oshpark.com/wp-content/uploads/2018/10/webrepl-browser.png?w=656&amp;h=516&amp;ssl=1" alt="WebREPL" /></p>

<p>ESP32 datasheet - <a href="https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf">https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf</a></p>

<hr />

<h1 id="how-to-setup-c-development-for-esp32">How to Setup C++ Development for ESP32?</h1>

<ol>
  <li>
    <p>Install Python (for more check <code class="language-plaintext highlighter-rouge">https://docs.espressif.com/projects/esp-idf/en/stable/esp32/get-started/linux-macos-setup.html#get-started-prerequisites</code>)</p>
  </li>
  <li>
    <p>Install ESP-IDF on your system. <strong>Github</strong> : <code class="language-plaintext highlighter-rouge">https://github.com/espressif/esp-idf</code> or Download latest esp-idf zip.</p>
  </li>
</ol>

<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="nb">cd </span>esp-idf
</pre></td></tr></tbody></table></code></pre></div></div>

<ol>
  <li>Run <code class="language-plaintext highlighter-rouge">./install.sh all</code>. After successful installation:
<em>Issue you might encounter - doesn’t trust the SSL certificate authority chain when trying to download toolchains for ESP-IDF.</em></li>
</ol>

<p>To fix this run these commands:</p>

<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre>/Applications/Python<span class="se">\ </span>3.13/Install<span class="se">\ </span>Certificates.command
</pre></td></tr></tbody></table></code></pre></div></div>

<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre>/Library/Frameworks/Python.framework/Versions/3.13/bin/python3 <span class="nt">-m</span> pip <span class="nb">install</span> <span class="nt">--upgrade</span> pip certifi
</pre></td></tr></tbody></table></code></pre></div></div>

<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre>python3 <span class="nt">-m</span> ssl
</pre></td></tr></tbody></table></code></pre></div></div>
<p>If you don’t see errors, SSL is now configured correctly.</p>

<ol>
  <li>Then activate the ESP-IDF environment (adds toolchain to your path):</li>
</ol>

<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="nb">source </span>export.sh
</pre></td></tr></tbody></table></code></pre></div></div>]]></content><author><name>Email me at</name><email>gls.prasadraju@gmail.com</email></author><category term="ESP32" /><category term="MicroPython" /><category term="C++" /><category term="IoT" /><category term="Microcontroller" /><summary type="html"><![CDATA[A comprehensive guide on working with the ESP32 microcontroller, including hardware specs, software tools like ESP-IDF, and programming with MicroPython and C++.]]></summary></entry></feed>