<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Rakesh Reddy Peddamallu</title>
    <description>The latest articles on Forem by Rakesh Reddy Peddamallu (@rakeshreddy512).</description>
    <link>https://forem.com/rakeshreddy512</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1107828%2F367c29b4-28b9-4e24-8008-998700b62ef8.png</url>
      <title>Forem: Rakesh Reddy Peddamallu</title>
      <link>https://forem.com/rakeshreddy512</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/rakeshreddy512"/>
    <language>en</language>
    <item>
      <title>complete path to becoming a professional UI engineer</title>
      <dc:creator>Rakesh Reddy Peddamallu</dc:creator>
      <pubDate>Tue, 14 Apr 2026 06:55:18 +0000</pubDate>
      <link>https://forem.com/rakeshreddy512/complete-path-to-becoming-a-professional-ui-engineer-1bcl</link>
      <guid>https://forem.com/rakeshreddy512/complete-path-to-becoming-a-professional-ui-engineer-1bcl</guid>
      <description>&lt;p&gt;Here's your complete path to becoming a professional UI engineer, broken down into phases:&lt;/p&gt;




&lt;h2&gt;
  
  
  Phase 1 — Foundations (2–3 months)
&lt;/h2&gt;

&lt;p&gt;This is non-negotiable ground. Everything else builds on this. Don't rush it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;HTML:&lt;/strong&gt; Semantic elements, forms and validation, accessibility basics, ARIA roles, the DOM structure, data attributes, meta tags.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CSS:&lt;/strong&gt; The box model, specificity and the cascade, Flexbox, CSS Grid, positioning (static, relative, absolute, fixed, sticky), units (px, rem, em, %, vh/vw), pseudo-classes and pseudo-elements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Vanilla JavaScript:&lt;/strong&gt; Variables, functions, loops, arrays, objects, DOM manipulation, event handling, the basics of async (callbacks → promises → async/await).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Project to build:&lt;/strong&gt; A fully static multi-page personal portfolio site — no frameworks, just HTML, CSS, and vanilla JS.&lt;/p&gt;




&lt;h2&gt;
  
  
  Phase 2 — Core JavaScript Depth (1–2 months)
&lt;/h2&gt;

&lt;p&gt;You need to actually understand JS before jumping into React, or you'll be copy-pasting code you don't understand.&lt;/p&gt;

&lt;p&gt;Closures, scope, and the &lt;code&gt;this&lt;/code&gt; keyword. The event loop and how the browser executes code. Promises and &lt;code&gt;async/await&lt;/code&gt; in depth. The Fetch API and working with REST APIs. ES modules (&lt;code&gt;import&lt;/code&gt;/&lt;code&gt;export&lt;/code&gt;). Array methods: &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt;, &lt;code&gt;reduce&lt;/code&gt;, &lt;code&gt;find&lt;/code&gt;. Error handling with &lt;code&gt;try/catch&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Project to build:&lt;/strong&gt; A weather app or movie search app that fetches real data from a public API, displays results, handles loading and error states — all in vanilla JS.&lt;/p&gt;




&lt;h2&gt;
  
  
  Phase 3 — React &amp;amp; Component Thinking (2–3 months)
&lt;/h2&gt;

&lt;p&gt;React is the industry standard. Learn it deeply, not just the surface.&lt;/p&gt;

&lt;p&gt;JSX syntax. Components, props, and state with &lt;code&gt;useState&lt;/code&gt;. Side effects with &lt;code&gt;useEffect&lt;/code&gt;. Conditional rendering and lists. Lifting state up and prop drilling. &lt;code&gt;useContext&lt;/code&gt; for shared state. &lt;code&gt;useRef&lt;/code&gt; for DOM access. Custom hooks. React Router for multi-page apps. Fetching data inside components, handling loading and error states.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Project to build:&lt;/strong&gt; A full CRUD app — something like a task manager or expense tracker — with multiple routes, persistent data via &lt;code&gt;localStorage&lt;/code&gt;, and clean component structure.&lt;/p&gt;




&lt;h2&gt;
  
  
  Phase 4 — Styling Systems &amp;amp; Design (1–2 months)
&lt;/h2&gt;

&lt;p&gt;This is what separates developers who can code from developers who can build beautiful products.&lt;/p&gt;

&lt;p&gt;CSS variables and theming. Responsive design and media queries properly. Mobile-first thinking. Tailwind CSS — understand the utility-first approach. CSS Modules for scoped styles. Basic design principles: spacing, typography scale, color contrast, visual hierarchy. Dark mode implementation. How to read and implement a Figma design accurately.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Project to build:&lt;/strong&gt; Rebuild an existing well-designed website (like Stripe's landing page or a popular SaaS product) as a pixel-faithful clone using React and Tailwind.&lt;/p&gt;




&lt;h2&gt;
  
  
  Phase 5 — Professional Tooling &amp;amp; TypeScript (1–2 months)
&lt;/h2&gt;

&lt;p&gt;This is what makes you functional in a real team codebase.&lt;/p&gt;

&lt;p&gt;TypeScript: types, interfaces, generics, typing React components and hooks. Vite as a build tool — understand what bundlers do. ESLint and Prettier — linting and formatting. npm and managing dependencies. Environment variables. Git beyond the basics: branching, rebasing, pull requests, resolving merge conflicts. Writing readable commit messages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Project to build:&lt;/strong&gt; Convert your Phase 3 CRUD app entirely to TypeScript. Fix every type error without using &lt;code&gt;any&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Phase 6 — State Management &amp;amp; Data Fetching (1 month)
&lt;/h2&gt;

&lt;p&gt;Once apps grow, local state isn't enough.&lt;/p&gt;

&lt;p&gt;When you need global state and when you don't. Zustand as a lightweight state manager — learn this before Redux. React Query (TanStack Query) for server state — caching, refetching, pagination, optimistic updates. The difference between client state and server state. Basic GraphQL concepts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Project to build:&lt;/strong&gt; A more complex app with real API integration — like a GitHub profile explorer or a Hacker News clone — using React Query for all data fetching.&lt;/p&gt;




&lt;h2&gt;
  
  
  Phase 7 — Performance &amp;amp; Quality (1–2 months)
&lt;/h2&gt;

&lt;p&gt;This is where junior ends and mid/senior begins.&lt;/p&gt;

&lt;p&gt;Core Web Vitals: LCP, CLS, FID/INP — know what they measure and how to improve them. Lazy loading images and components with &lt;code&gt;React.lazy&lt;/code&gt; and &lt;code&gt;Suspense&lt;/code&gt;. &lt;code&gt;useMemo&lt;/code&gt; and &lt;code&gt;useCallback&lt;/code&gt; — know when they help and when they're overkill. List virtualization for large datasets. Debouncing and throttling. Code splitting and bundle analysis. Using Chrome DevTools to profile and find bottlenecks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Testing:&lt;/strong&gt; Unit testing with Vitest/Jest. Component testing with React Testing Library. End-to-end testing with Playwright. The testing pyramid: know what to test at each level.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Project to build:&lt;/strong&gt; Audit one of your previous projects using Chrome DevTools and Lighthouse. Fix every performance issue you find. Add a full test suite.&lt;/p&gt;




&lt;h2&gt;
  
  
  Phase 8 — Next.js &amp;amp; Production Thinking (1–2 months)
&lt;/h2&gt;

&lt;p&gt;Most professional React jobs use Next.js or a similar framework.&lt;/p&gt;

&lt;p&gt;SSR vs SSG vs CSR — know the tradeoffs deeply. The Next.js App Router. Server Components vs Client Components. Route handlers and API routes. Image optimization. Environment configuration. Deployment to Vercel. SEO basics: meta tags, Open Graph, structured data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Project to build:&lt;/strong&gt; A full-stack Next.js app deployed to production — a blog with a CMS, or a SaaS landing page with a contact form backed by a real database.&lt;/p&gt;




&lt;h2&gt;
  
  
  Phase 9 — Accessibility &amp;amp; Security (ongoing)
&lt;/h2&gt;

&lt;p&gt;Most developers ignore these. Mastering them makes you stand out.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Accessibility:&lt;/strong&gt; WCAG AA compliance. Keyboard navigation and focus management. Screen reader testing (use VoiceOver or NVDA). Color contrast requirements. Accessible forms, modals, and custom components.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Security:&lt;/strong&gt; XSS prevention — never trust user input. CORS and why it exists. Authentication patterns: JWT, sessions, OAuth flows. Content Security Policy headers. CSRF basics.&lt;/p&gt;




&lt;h2&gt;
  
  
  Phase 10 — Collaboration &amp;amp; Career Skills (ongoing)
&lt;/h2&gt;

&lt;p&gt;The skills that actually get you hired and promoted.&lt;/p&gt;

&lt;p&gt;Reading other people's code without getting lost. Writing clear pull request descriptions. Giving and receiving code review feedback. Breaking a vague design into a component tree. Estimating tasks (badly at first, then less badly). Communicating blockers early. Writing documentation that future-you will thank present-you for.&lt;/p&gt;




&lt;h2&gt;
  
  
  Honest timeline
&lt;/h2&gt;

&lt;p&gt;If you study and build consistently (3–4 hours a day): &lt;strong&gt;12–18 months&lt;/strong&gt; to be genuinely hireable as a junior-to-mid UI engineer. Phases 1–6 get you to hireable. Phases 7–10 get you to strong mid-level.&lt;/p&gt;

&lt;p&gt;The single biggest mistake people make is spending too long in tutorial mode and not enough time building real things that break in ways tutorials don't prepare you for. After Phase 2, every phase should be 40% learning and 60% building.&lt;/p&gt;

&lt;p&gt;Want me to go deep on any specific phase?&lt;/p&gt;

</description>
    </item>
    <item>
      <title>concepts to learn</title>
      <dc:creator>Rakesh Reddy Peddamallu</dc:creator>
      <pubDate>Sat, 11 Apr 2026 17:43:29 +0000</pubDate>
      <link>https://forem.com/rakeshreddy512/concepts-to-learn-2f4i</link>
      <guid>https://forem.com/rakeshreddy512/concepts-to-learn-2f4i</guid>
      <description>&lt;h1&gt;
  
  
  🧠 1. JavaScript (your real foundation — React is just a layer)
&lt;/h1&gt;

&lt;p&gt;If this is shaky, everything collapses.&lt;/p&gt;

&lt;h3&gt;
  
  
  Core concepts
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Closures (not definition — real use cases)&lt;/li&gt;
&lt;li&gt;Scope chain&lt;/li&gt;
&lt;li&gt;Hoisting (functions vs variables vs let/const)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;this&lt;/code&gt; (call, apply, bind)&lt;/li&gt;
&lt;li&gt;Prototypes &amp;amp; inheritance&lt;/li&gt;
&lt;li&gt;Event loop (microtasks vs macrotasks)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 You should be able to explain:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Why does Promise run before setTimeout?”&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  Async mastery
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Promises chaining&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;async/await&lt;/code&gt; vs &lt;code&gt;.then&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Error handling patterns&lt;/li&gt;
&lt;li&gt;Parallel vs sequential execution (&lt;code&gt;Promise.all&lt;/code&gt;, etc.)&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Advanced JS (this is where mid-level → senior happens)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Debouncing &amp;amp; throttling (implement from scratch)&lt;/li&gt;
&lt;li&gt;Deep vs shallow copy&lt;/li&gt;
&lt;li&gt;Currying&lt;/li&gt;
&lt;li&gt;Memoization&lt;/li&gt;
&lt;li&gt;Immutability (why it matters in React)&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  ⚛️ 2. React Core (but &lt;em&gt;deep&lt;/em&gt;, not surface level)
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Rendering &amp;amp; lifecycle
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;What triggers re-render?&lt;/li&gt;
&lt;li&gt;Parent → child render flow&lt;/li&gt;
&lt;li&gt;Reconciliation algorithm (high level is fine)&lt;/li&gt;
&lt;li&gt;Keys (why index is bad — but also when it's okay)&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Hooks (this is where interviews focus heavily)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;useEffect&lt;/code&gt; (timing, cleanup, dependency traps)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;useState&lt;/code&gt; batching behavior&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;useRef&lt;/code&gt; (why it doesn’t trigger re-render)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;useMemo&lt;/code&gt; vs &lt;code&gt;useCallback&lt;/code&gt; vs &lt;code&gt;React.memo&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 You should clearly answer:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Why is my useEffect running twice?” (React 18 Strict Mode)&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  Advanced patterns
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Controlled vs uncontrolled components&lt;/li&gt;
&lt;li&gt;Compound components pattern&lt;/li&gt;
&lt;li&gt;Render props&lt;/li&gt;
&lt;li&gt;Higher Order Components (HOC)&lt;/li&gt;
&lt;li&gt;Custom hooks (design clean APIs)&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  ⚡ 3. React Performance (high signal topic)
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Avoiding unnecessary re-renders&lt;/li&gt;
&lt;li&gt;Memoization strategy (not overusing it)&lt;/li&gt;
&lt;li&gt;List rendering optimization&lt;/li&gt;
&lt;li&gt;Virtualization (like &lt;code&gt;react-window&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Code splitting (&lt;code&gt;React.lazy&lt;/code&gt;, Suspense)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 Strong answer:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“How do you debug performance issues in React?”&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  🏗️ 4. State Management (you need opinions here)
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Local vs global state&lt;/li&gt;
&lt;li&gt;Context API (and its limitations)&lt;/li&gt;
&lt;li&gt;Redux (why, when, when NOT)&lt;/li&gt;
&lt;li&gt;Zustand / lightweight stores&lt;/li&gt;
&lt;li&gt;Server state vs client state (React Query mindset)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 You should say:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“I wouldn’t use Redux here because…”&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  🌐 5. API &amp;amp; Data Layer
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;REST vs GraphQL (basic tradeoffs)&lt;/li&gt;
&lt;li&gt;Caching strategies&lt;/li&gt;
&lt;li&gt;Error handling patterns&lt;/li&gt;
&lt;li&gt;Request cancellation (AbortController)&lt;/li&gt;
&lt;li&gt;Pagination / infinite scroll&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  🎨 6. Browser &amp;amp; UI Fundamentals (people ignore this and suffer)
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Browser internals
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;DOM vs Virtual DOM&lt;/li&gt;
&lt;li&gt;Reflow &amp;amp; Repaint&lt;/li&gt;
&lt;li&gt;CSS specificity&lt;/li&gt;
&lt;li&gt;Layout systems (Flexbox, Grid)&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Performance basics
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Critical rendering path&lt;/li&gt;
&lt;li&gt;Lazy loading images/components&lt;/li&gt;
&lt;li&gt;Debouncing user input (search bars etc.)&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  🧩 7. System Design (frontend version)
&lt;/h1&gt;

&lt;p&gt;This is your &lt;strong&gt;SDE-2 differentiator&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You should be able to design:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dashboard UI (you literally built one — use it)&lt;/li&gt;
&lt;li&gt;Chat app (you’re working on it — perfect)&lt;/li&gt;
&lt;li&gt;Data-heavy table (filters, sorting, pagination)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Focus on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Component structure&lt;/li&gt;
&lt;li&gt;State flow&lt;/li&gt;
&lt;li&gt;API handling&lt;/li&gt;
&lt;li&gt;Performance tradeoffs&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  🧪 8. Testing (at least decent level)
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Unit testing (Jest)&lt;/li&gt;
&lt;li&gt;React Testing Library&lt;/li&gt;
&lt;li&gt;Mocking APIs&lt;/li&gt;
&lt;li&gt;Testing user behavior (not implementation)&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  🔐 9. Auth &amp;amp; Security (VERY underrated)
&lt;/h1&gt;

&lt;p&gt;Since you worked on IAM — use it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JWT vs session&lt;/li&gt;
&lt;li&gt;Token refresh flow&lt;/li&gt;
&lt;li&gt;Secure storage (cookies vs localStorage)&lt;/li&gt;
&lt;li&gt;XSS / CSRF basics&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  🧱 10. Architecture &amp;amp; Real-world Thinking
&lt;/h1&gt;

&lt;p&gt;This is where you stand out.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Folder structure&lt;/li&gt;
&lt;li&gt;Reusable components&lt;/li&gt;
&lt;li&gt;Separation of concerns&lt;/li&gt;
&lt;li&gt;API abstraction layer&lt;/li&gt;
&lt;li&gt;Microfrontends (you’ve touched this — leverage it)&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  ⚠️ Brutal truth (you’ll appreciate this)
&lt;/h1&gt;

&lt;p&gt;Most 4-year devs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Know hooks ✅&lt;/li&gt;
&lt;li&gt;Can build UI ✅&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cannot explain WHY things work ❌&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That’s exactly what interviews test.&lt;/p&gt;




&lt;h1&gt;
  
  
  🧭 Simple way to check yourself
&lt;/h1&gt;

&lt;p&gt;If you can confidently answer questions like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;“Why does React re-render?”&lt;/li&gt;
&lt;li&gt;“How does event loop work?”&lt;/li&gt;
&lt;li&gt;“When would you NOT use useEffect?”&lt;/li&gt;
&lt;li&gt;“How would you design a scalable table?”&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 You’re in top ~15%.&lt;/p&gt;




&lt;h1&gt;
  
  
  💥 My opinion on you (based on your work)
&lt;/h1&gt;

&lt;p&gt;You already have an edge:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dashboard work&lt;/li&gt;
&lt;li&gt;IAM flow&lt;/li&gt;
&lt;li&gt;Complex UI&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you package that into &lt;strong&gt;clear explanations&lt;/strong&gt;, you’ll crack strong companies.&lt;/p&gt;




&lt;p&gt;If you want next level:&lt;br&gt;
I can give you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;🔥 30 &lt;strong&gt;real interview questions&lt;/strong&gt; (React + JS + design)&lt;/li&gt;
&lt;li&gt;🎯 or do a &lt;strong&gt;mock interview and expose gaps&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Just tell me what you want.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Leetcode - 23. Merge k Sorted Lists</title>
      <dc:creator>Rakesh Reddy Peddamallu</dc:creator>
      <pubDate>Mon, 15 Sep 2025 16:29:19 +0000</pubDate>
      <link>https://forem.com/rakeshreddy512/leetcode-23-merge-k-sorted-lists-28hf</link>
      <guid>https://forem.com/rakeshreddy512/leetcode-23-merge-k-sorted-lists-28hf</guid>
      <description>&lt;p&gt;When working with linked lists, a classic problem you’ll encounter is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;“Given &lt;code&gt;k&lt;/code&gt; sorted linked lists, merge them into one sorted linked list and return it.”&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This problem is a favorite in interviews (especially LeetCode #23) because it combines understanding of linked lists, divide-and-conquer, and complexity trade-offs. Let’s dive in step by step.&lt;/p&gt;




&lt;h2&gt;
  
  
  🧩 Problem Breakdown
&lt;/h2&gt;

&lt;p&gt;We’re given &lt;code&gt;k&lt;/code&gt; linked lists, each individually sorted. Our task is to merge them into one sorted linked list.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input:
lists = [
  1 -&amp;gt; 4 -&amp;gt; 5,
  1 -&amp;gt; 3 -&amp;gt; 4,
  2 -&amp;gt; 6
]

Output:
1 -&amp;gt; 1 -&amp;gt; 2 -&amp;gt; 3 -&amp;gt; 4 -&amp;gt; 4 -&amp;gt; 5 -&amp;gt; 6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  💡 Approach
&lt;/h2&gt;

&lt;p&gt;There are multiple ways to solve this problem:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Brute Force:&lt;/strong&gt; Put all nodes into an array, sort, then rebuild a linked list.&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Simple, but sorting all nodes takes &lt;strong&gt;O(N log N)&lt;/strong&gt; time, where N is the total number of nodes.&lt;/li&gt;
&lt;li&gt;Not very elegant.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Min-Heap / Priority Queue:&lt;/strong&gt; Keep all heads of the lists in a heap, repeatedly pop the smallest.&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Efficient (&lt;strong&gt;O(N log k)&lt;/strong&gt;) but requires a heap implementation.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Divide and Conquer (our approach):&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Repeatedly merge pairs of lists (like merge sort).&lt;/li&gt;
&lt;li&gt;Each merge is linear, and we keep halving the number of lists until one remains.&lt;/li&gt;
&lt;li&gt;Clean and efficient without extra data structures.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We’ll use &lt;strong&gt;divide and conquer&lt;/strong&gt; here.&lt;/p&gt;




&lt;h2&gt;
  
  
  🛠 Step 1: Merging Two Sorted Lists
&lt;/h2&gt;

&lt;p&gt;We first need a helper to merge &lt;strong&gt;two sorted linked lists&lt;/strong&gt;. This is like the merge step of merge sort.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;merge_two_lists&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;list1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;list2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;dummy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ListNode&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;dummy&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;list1&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;list2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;list1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;list2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;list1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nx"&gt;list1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;list1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;list2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nx"&gt;list2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;list2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;list1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;list1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;list2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;list2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;dummy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This runs in &lt;strong&gt;O(n + m)&lt;/strong&gt; where &lt;code&gt;n&lt;/code&gt; and &lt;code&gt;m&lt;/code&gt; are the lengths of the two lists.&lt;/p&gt;




&lt;h2&gt;
  
  
  🛠 Step 2: Merging K Lists Using Divide and Conquer
&lt;/h2&gt;

&lt;p&gt;We now merge lists &lt;strong&gt;pair by pair&lt;/strong&gt; until only one list remains.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;mergeKLists&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;lists&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;lists&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;lists&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;lists&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;mergedLists&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;

        &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;lists&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;l1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;lists&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
            &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;l2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;lists&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;lists&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nx"&gt;mergedLists&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;merge_two_lists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;l1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;l2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="nx"&gt;lists&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mergedLists&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;lists&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  📊 Complexity Analysis
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Merging Two Lists:&lt;/strong&gt; O(n + m)&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Merging K Lists (Divide &amp;amp; Conquer):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each round halves the number of lists.&lt;/li&gt;
&lt;li&gt;We perform &lt;code&gt;log k&lt;/code&gt; rounds.&lt;/li&gt;
&lt;li&gt;Each node is processed in every merge step, so total work is &lt;strong&gt;O(N log k)&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;N&lt;/code&gt; = total number of nodes across all lists&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;k&lt;/code&gt; = number of lists&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Space Complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;We only use a few pointers and a dummy node.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;O(1)&lt;/strong&gt; extra space (not counting recursion stack if you write a recursive merge).&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ✅ Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;divide and conquer approach&lt;/strong&gt; is elegant, efficient, and doesn’t require additional data structures.&lt;/li&gt;
&lt;li&gt;Time complexity: &lt;strong&gt;O(N log k)&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Space complexity: &lt;strong&gt;O(1)&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you need &lt;strong&gt;even faster performance in practice&lt;/strong&gt;, a &lt;strong&gt;priority queue (min-heap)&lt;/strong&gt; is often used, especially when &lt;code&gt;k&lt;/code&gt; is very large. But for clean code and interviews, this divide-and-conquer approach is a winner.&lt;/p&gt;




</description>
      <category>leetcode</category>
    </item>
    <item>
      <title>Leetcode - 427. Construct Quad Tree</title>
      <dc:creator>Rakesh Reddy Peddamallu</dc:creator>
      <pubDate>Tue, 09 Sep 2025 16:26:18 +0000</pubDate>
      <link>https://forem.com/rakeshreddy512/leetcode-427-construct-quad-tree-5eej</link>
      <guid>https://forem.com/rakeshreddy512/leetcode-427-construct-quad-tree-5eej</guid>
      <description>&lt;p&gt;QuadTrees are a fascinating data structure, often used in &lt;strong&gt;image compression, spatial indexing, and graphics&lt;/strong&gt;. In this blog, we’ll solve the &lt;strong&gt;LeetCode 427 – Construct Quad Tree&lt;/strong&gt; problem, step by step.&lt;/p&gt;




&lt;h2&gt;
  
  
  📌 Problem Statement
&lt;/h2&gt;

&lt;p&gt;You are given an &lt;code&gt;n x n&lt;/code&gt; binary grid (only &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;1&lt;/code&gt; values).&lt;/p&gt;

&lt;p&gt;Your task is to construct a &lt;strong&gt;QuadTree&lt;/strong&gt; based on this grid:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each node in the tree represents a sub-square of the grid.&lt;/li&gt;
&lt;li&gt;If the sub-square contains all the same values (&lt;code&gt;0&lt;/code&gt; or &lt;code&gt;1&lt;/code&gt;), it becomes a &lt;strong&gt;leaf node&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Otherwise, it splits into &lt;strong&gt;four children&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;topLeft&lt;/li&gt;
&lt;li&gt;topRight&lt;/li&gt;
&lt;li&gt;bottomLeft&lt;/li&gt;
&lt;li&gt;bottomRight&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  📖 What is a QuadTree?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;QuadTree&lt;/strong&gt; recursively divides a 2D space into four quadrants.&lt;/p&gt;

&lt;p&gt;For example, take this &lt;code&gt;4x4&lt;/code&gt; binary grid:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The top-left 2x2 block is all &lt;code&gt;1&lt;/code&gt; → becomes a &lt;strong&gt;leaf node&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The top-right 2x2 block is all &lt;code&gt;0&lt;/code&gt; → another &lt;strong&gt;leaf node&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The bottom-left 2x2 block is all &lt;code&gt;0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The bottom-right 2x2 block is all &lt;code&gt;1&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So the root node will &lt;strong&gt;split into four leaves&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  🛠️ Approach
&lt;/h2&gt;

&lt;p&gt;We’ll use &lt;strong&gt;DFS (divide and conquer)&lt;/strong&gt; to recursively build the tree:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Check if the current sub-square is uniform&lt;/strong&gt; (all values are the same).&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;If yes → return a &lt;strong&gt;leaf node&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;If not → split into four quadrants.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Recursive Subdivision&lt;/strong&gt;:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Divide the current square of size &lt;code&gt;n&lt;/code&gt; into four &lt;code&gt;n/2&lt;/code&gt; quadrants.&lt;/li&gt;
&lt;li&gt;Recursively construct the QuadTree for each.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Build the Node&lt;/strong&gt;:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;If it’s a leaf → &lt;code&gt;isLeaf = true&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If not → &lt;code&gt;isLeaf = false&lt;/code&gt; and link four children.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ✅ Solution Code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * // Definition for a QuadTree node.
 * function Node(val, isLeaf, topLeft, topRight, bottomLeft, bottomRight) {
 *    this.val = val;
 *    this.isLeaf = isLeaf;
 *    this.topLeft = topLeft;
 *    this.topRight = topRight;
 *    this.bottomLeft = bottomLeft;
 *    this.bottomRight = bottomRight;
 * };
 */&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * @param {number[][]} grid
 * @return {Node}
 */&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;construct&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dfs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;isAllSame&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;firstVal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

        &lt;span class="c1"&gt;// check if all values in current n x n block are the same&lt;/span&gt;
        &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;firstVal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="nx"&gt;isAllSame&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                    &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;isAllSame&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// case 1: all same → leaf node&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isAllSame&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstVal&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// case 2: not uniform → divide into 4 parts&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;half&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;topLeftNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;half&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;topRightNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;half&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;half&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;bottomLeftNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;half&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;half&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;bottomRightNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;half&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;half&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;half&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;topLeftNode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;topRightNode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;bottomLeftNode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;bottomRightNode&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  ⏱️ Complexity Analysis
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Time Complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;At each level, we scan an &lt;code&gt;n x n&lt;/code&gt; block to check if it’s uniform.&lt;/li&gt;
&lt;li&gt;Then we split into &lt;strong&gt;4 subproblems&lt;/strong&gt; of size &lt;code&gt;n/2&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So recurrence:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T(n) = 4T(n/2) + O(n^2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This looks like &lt;strong&gt;O(n^2 log n)&lt;/strong&gt; in the worst case.&lt;br&gt;
But since each cell is checked only once in practice, the overall complexity simplifies to &lt;strong&gt;O(n^2)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;👉 This is optimal since we must inspect every cell at least once.&lt;/p&gt;

&lt;h3&gt;
  
  
  Space Complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The recursion depth is &lt;code&gt;O(log n)&lt;/code&gt; (each time we halve &lt;code&gt;n&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;The QuadTree itself can take up to &lt;code&gt;O(n^2)&lt;/code&gt; nodes in the worst case (checkerboard pattern).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So space complexity is &lt;strong&gt;O(n^2)&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  🎯 Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;QuadTrees are great for representing &lt;strong&gt;2D uniform data&lt;/strong&gt; efficiently.&lt;/li&gt;
&lt;li&gt;If a subgrid is uniform, we don’t need to store every cell → compression.&lt;/li&gt;
&lt;li&gt;Divide &amp;amp; conquer makes this problem elegant and intuitive.&lt;/li&gt;
&lt;/ul&gt;




</description>
      <category>leetcode</category>
    </item>
    <item>
      <title>Leetcode - 148. Sort List</title>
      <dc:creator>Rakesh Reddy Peddamallu</dc:creator>
      <pubDate>Mon, 01 Sep 2025 11:41:18 +0000</pubDate>
      <link>https://forem.com/rakeshreddy512/leetcode-148-sort-list-3ln6</link>
      <guid>https://forem.com/rakeshreddy512/leetcode-148-sort-list-3ln6</guid>
      <description>&lt;p&gt;Sorting a linked list isn’t as straightforward as sorting an array. With arrays, we can do quick sort or heap sort thanks to random access. But linked lists only allow &lt;strong&gt;sequential access&lt;/strong&gt;, making those methods inefficient.&lt;/p&gt;

&lt;p&gt;That’s where &lt;strong&gt;merge sort&lt;/strong&gt; shines. It works beautifully on linked lists because splitting and merging can be done in-place with pointers. Let’s dive in.&lt;/p&gt;




&lt;h2&gt;
  
  
  🔹 Problem Statement
&lt;/h2&gt;

&lt;p&gt;Given the head of a linked list, return the list sorted in ascending order.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input:  4 → 2 → 1 → 3
Output: 1 → 2 → 3 → 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🔹 Approach: Divide and Conquer
&lt;/h2&gt;

&lt;p&gt;We apply the merge sort strategy:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Divide&lt;/strong&gt;: Use the slow–fast pointer trick to find the middle and split the list into two halves.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Conquer&lt;/strong&gt;: Recursively sort both halves.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Combine&lt;/strong&gt;: Merge the two sorted halves back together.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  🔹 Step 1: Find the Middle
&lt;/h2&gt;

&lt;p&gt;We use two pointers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;slow&lt;/code&gt; moves one step at a time.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fast&lt;/code&gt; moves two steps at a time.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When &lt;code&gt;fast&lt;/code&gt; reaches the end, &lt;code&gt;slow&lt;/code&gt; will be at the &lt;strong&gt;middle&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getMid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;slow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;fast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// ensures we stop at the "first middle" for even length&lt;/span&gt;

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

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;slow&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// returns middle node&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🔹 Step 2: Merge Two Sorted Lists
&lt;/h2&gt;

&lt;p&gt;This step is similar to merging two sorted arrays:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;merge&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;list1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;list2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;dummy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ListNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;dummy&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;list1&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;list2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;list1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;list2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;list1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nx"&gt;list1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;list1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;list2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nx"&gt;list2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;list2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// attach the remaining part&lt;/span&gt;
    &lt;span class="nx"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;list1&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;list2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;dummy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🔹 Step 3: Recursive Merge Sort
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;sortList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// base case&lt;/span&gt;

    &lt;span class="c1"&gt;// 1. Split into two halves&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getMid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// break the link&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// 2. Recursively sort both halves&lt;/span&gt;
    &lt;span class="nx"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;sortList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;sortList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// 3. Merge the sorted halves&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;merge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🔹 Walkthrough Example: &lt;code&gt;4 → 2 → 1 → 3&lt;/code&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step 1: Initial Split
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Mid = &lt;code&gt;2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Left = &lt;code&gt;4 → 2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Right = &lt;code&gt;1 → 3&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Step 2: Sort Left (&lt;code&gt;4 → 2&lt;/code&gt;)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Mid = &lt;code&gt;4&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Left = &lt;code&gt;4&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Right = &lt;code&gt;2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Merge → &lt;code&gt;2 → 4&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Step 3: Sort Right (&lt;code&gt;1 → 3&lt;/code&gt;)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Mid = &lt;code&gt;1&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Left = &lt;code&gt;1&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Right = &lt;code&gt;3&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Merge → &lt;code&gt;1 → 3&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Step 4: Merge the Halves
&lt;/h3&gt;

&lt;p&gt;Merge &lt;code&gt;2 → 4&lt;/code&gt; and &lt;code&gt;1 → 3&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Compare 2 vs 1 → pick 1&lt;/li&gt;
&lt;li&gt;Compare 2 vs 3 → pick 2&lt;/li&gt;
&lt;li&gt;Compare 4 vs 3 → pick 3&lt;/li&gt;
&lt;li&gt;Leftover → 4&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Result: &lt;code&gt;1 → 2 → 3 → 4&lt;/code&gt; ✅&lt;/p&gt;




&lt;h3&gt;
  
  
  Recursion Tree
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sort(4 2 1 3)
   /        \
sort(4 2)  sort(1 3)
  /   \      /   \
 4     2    1     3

Merges:
(4,2) → (2 4)
(1,3) → (1 3)
(2 4) + (1 3) → (1 2 3 4)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🔹 Complexity Analysis
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Time Complexity:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Splitting takes &lt;code&gt;O(log n)&lt;/code&gt; levels.&lt;/li&gt;
&lt;li&gt;Each merge across levels costs &lt;code&gt;O(n)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Overall: &lt;strong&gt;O(n log n)&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Space Complexity:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Merge happens in-place with pointers → &lt;code&gt;O(1)&lt;/code&gt; extra space.&lt;/li&gt;
&lt;li&gt;Recursion depth = &lt;code&gt;O(log n)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔹 Why Merge Sort Works Best for Linked Lists?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Quick sort needs random access (bad for linked lists).&lt;/li&gt;
&lt;li&gt;Merge sort only needs sequential traversal (perfect fit).&lt;/li&gt;
&lt;li&gt;Splitting and merging are pointer operations, so memory overhead is minimal.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ✅ Final Takeaway
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;slow–fast pointers&lt;/strong&gt; to find the middle.&lt;/li&gt;
&lt;li&gt;Recursively sort both halves.&lt;/li&gt;
&lt;li&gt;Merge with pointer juggling.&lt;/li&gt;
&lt;li&gt;Achieve &lt;strong&gt;O(n log n)&lt;/strong&gt; efficiency with minimal space overhead.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 Next time someone asks “How do you sort a linked list?”, you’ve got a clean, interview-ready answer. 🚀&lt;/p&gt;




</description>
      <category>leetcode</category>
    </item>
    <item>
      <title>Backend Interview Prep Beyond LeetCode (For 3 Years Experience)</title>
      <dc:creator>Rakesh Reddy Peddamallu</dc:creator>
      <pubDate>Sun, 31 Aug 2025 18:29:33 +0000</pubDate>
      <link>https://forem.com/rakeshreddy512/backend-interview-prep-beyond-leetcode-for-3-years-experience-7gf</link>
      <guid>https://forem.com/rakeshreddy512/backend-interview-prep-beyond-leetcode-for-3-years-experience-7gf</guid>
      <description>&lt;p&gt;Most backend engineers spend months grinding LeetCode. But in reality, &lt;strong&gt;backend interviews are about much more than algorithms.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;At 3 years of experience, interviewers want to know if you can design reliable APIs, work with databases efficiently, and build systems that scale.&lt;/p&gt;

&lt;p&gt;Here’s the complete roadmap to prepare for backend interviews apart from DSA.&lt;/p&gt;




&lt;h2&gt;
  
  
  🔑 1. Core Language &amp;amp; Coding Skills
&lt;/h2&gt;

&lt;p&gt;You need to be solid in the language you’ll interview in (Java, Python, Go, Node.js, etc.).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Data structures: arrays, maps, sets, queues&lt;/li&gt;
&lt;li&gt;Object-oriented principles (SOLID, design patterns)&lt;/li&gt;
&lt;li&gt;Concurrency, async programming (threads, promises, goroutines, etc.)&lt;/li&gt;
&lt;li&gt;Error handling, logging, testing practices&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;📚 &lt;strong&gt;Resources&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Effective Java&lt;/em&gt; (if in Java)&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Design Patterns&lt;/em&gt; (Gang of Four book)&lt;/li&gt;
&lt;li&gt;Language-specific docs&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🌐 2. APIs &amp;amp; Web Fundamentals
&lt;/h2&gt;

&lt;p&gt;Since most backend = APIs, expect deep questions here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;REST vs GraphQL (tradeoffs)&lt;/li&gt;
&lt;li&gt;Authentication &amp;amp; Authorization (JWT, OAuth2, sessions)&lt;/li&gt;
&lt;li&gt;Rate limiting, pagination, idempotency&lt;/li&gt;
&lt;li&gt;Caching strategies (client, CDN, server-side)&lt;/li&gt;
&lt;li&gt;API versioning &amp;amp; backward compatibility&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;📚 &lt;strong&gt;Resources&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/azure/architecture/best-practices/api-design" rel="noopener noreferrer"&gt;RESTful API design guidelines by Microsoft&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Designing APIs with Swagger and OpenAPI&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🗄️ 3. Databases &amp;amp; Storage
&lt;/h2&gt;

&lt;p&gt;Databases are the heart of backend systems.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;SQL: Joins, indexes, transactions, normalization vs denormalization&lt;/li&gt;
&lt;li&gt;NoSQL: When to use MongoDB, Redis, DynamoDB&lt;/li&gt;
&lt;li&gt;ACID vs BASE&lt;/li&gt;
&lt;li&gt;Database sharding, replication, partitioning&lt;/li&gt;
&lt;li&gt;Query optimization (EXPLAIN, indexing strategies)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;📚 &lt;strong&gt;Resources&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;SQL Antipatterns&lt;/em&gt; (Bill Karwin)&lt;/li&gt;
&lt;li&gt;MongoDB University (free courses)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ⚡ 4. System Design (Backend-Oriented)
&lt;/h2&gt;

&lt;p&gt;The big one: they’ll test your ability to design systems, not just functions.&lt;/p&gt;

&lt;p&gt;Common questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Design a URL shortener (TinyURL)&lt;/li&gt;
&lt;li&gt;Design an e-commerce backend (cart, checkout, inventory)&lt;/li&gt;
&lt;li&gt;Design a rate limiter&lt;/li&gt;
&lt;li&gt;Design a chat service (like WhatsApp)&lt;/li&gt;
&lt;li&gt;Design logging/metrics pipeline&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Focus on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Scalability: load balancers, horizontal vs vertical scaling&lt;/li&gt;
&lt;li&gt;Databases: choosing SQL vs NoSQL&lt;/li&gt;
&lt;li&gt;Message queues (Kafka, RabbitMQ, SQS)&lt;/li&gt;
&lt;li&gt;Caching (Redis, Memcached)&lt;/li&gt;
&lt;li&gt;Microservices vs monolith&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;📚 &lt;strong&gt;Resources&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;System Design Primer&lt;/em&gt; (GitHub repo)&lt;/li&gt;
&lt;li&gt;Educative’s &lt;em&gt;Grokking Modern System Design&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ⚙️ 5. Infrastructure &amp;amp; DevOps Basics
&lt;/h2&gt;

&lt;p&gt;You don’t need to be a DevOps engineer, but you must understand:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Containers &amp;amp; orchestration (Docker, Kubernetes basics)&lt;/li&gt;
&lt;li&gt;CI/CD pipelines&lt;/li&gt;
&lt;li&gt;Monitoring &amp;amp; logging (Prometheus, Grafana, ELK stack)&lt;/li&gt;
&lt;li&gt;Cloud basics (AWS/GCP/Azure services like S3, EC2, Lambda)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;📚 &lt;strong&gt;Resources&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;The Twelve-Factor App&lt;/em&gt; principles&lt;/li&gt;
&lt;li&gt;FreeCodeCamp YouTube (Docker &amp;amp; Kubernetes tutorials)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔒 6. Security Fundamentals
&lt;/h2&gt;

&lt;p&gt;Interviewers often check if you can build secure systems.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hashing, encryption (bcrypt, AES)&lt;/li&gt;
&lt;li&gt;SQL injection, XSS, CSRF&lt;/li&gt;
&lt;li&gt;HTTPS/TLS basics&lt;/li&gt;
&lt;li&gt;Secure password storage &amp;amp; authentication flows&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;📚 &lt;strong&gt;Resources&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;OWASP Top 10&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  💻 7. Practical Coding Rounds
&lt;/h2&gt;

&lt;p&gt;Beyond DSA, expect &lt;strong&gt;hands-on backend tasks&lt;/strong&gt; like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Build a REST API with CRUD &amp;amp; authentication&lt;/li&gt;
&lt;li&gt;Design a rate limiter middleware&lt;/li&gt;
&lt;li&gt;Build a message queue simulator&lt;/li&gt;
&lt;li&gt;Implement caching layer with Redis&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 &lt;strong&gt;Tip:&lt;/strong&gt; Practice these by building small services with your stack (Node.js/Express, Spring Boot, Django, etc.).&lt;/p&gt;




&lt;h2&gt;
  
  
  👥 8. Behavioral &amp;amp; Team Skills
&lt;/h2&gt;

&lt;p&gt;At 3 years, they expect:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ownership: &lt;em&gt;“Tell me how you debugged a production issue.”&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Tradeoffs: &lt;em&gt;“When would you use SQL vs NoSQL?”&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Collaboration with frontend &amp;amp; DevOps teams&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🛠 Suggested Roadmap
&lt;/h2&gt;

&lt;p&gt;Here’s a practical way to prepare:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Language + APIs&lt;/strong&gt; (2–3 weeks)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Databases&lt;/strong&gt; (SQL &amp;amp; NoSQL, 2 weeks)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;System Design (small to mid-scale)&lt;/strong&gt; (3 weeks)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Infra basics (Docker, caching, queues)&lt;/strong&gt; (2 weeks)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Build small backend projects&lt;/strong&gt; (apply your learning)&lt;/li&gt;
&lt;/ol&gt;




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

&lt;p&gt;DSA clears the initial bar, but backend interviews are about &lt;strong&gt;real-world engineering&lt;/strong&gt;: designing APIs, managing databases, scaling systems, and ensuring security.&lt;/p&gt;

&lt;p&gt;To stand out in a &lt;strong&gt;3 years backend interview&lt;/strong&gt;, focus on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;APIs &amp;amp; databases&lt;/strong&gt; (real-world problems)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;System design&lt;/strong&gt; (small to medium scale)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hands-on projects&lt;/strong&gt; (showing you can build, not just solve puzzles)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That’s how you show you’re not just a coder, but a &lt;strong&gt;backend engineer ready for scale.&lt;/strong&gt;&lt;/p&gt;




</description>
      <category>interview</category>
      <category>backend</category>
    </item>
    <item>
      <title>Frontend Interview Prep Beyond LeetCode (For 3 Years Experience)</title>
      <dc:creator>Rakesh Reddy Peddamallu</dc:creator>
      <pubDate>Sun, 31 Aug 2025 18:25:42 +0000</pubDate>
      <link>https://forem.com/rakeshreddy512/frontend-interview-prep-beyond-leetcode-for-3-years-experience-b0h</link>
      <guid>https://forem.com/rakeshreddy512/frontend-interview-prep-beyond-leetcode-for-3-years-experience-b0h</guid>
      <description>&lt;p&gt;Most developers preparing for frontend interviews drown themselves in LeetCode. While data structures and algorithms are important, &lt;strong&gt;frontend interviews go far beyond just solving DSA problems.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you’re preparing for a &lt;strong&gt;3 years experience role&lt;/strong&gt;, companies expect you to not only write clean code but also design scalable UIs, understand the browser deeply, and optimize for performance.&lt;/p&gt;

&lt;p&gt;Here’s a roadmap of what you should prepare apart from LeetCode, with the best resources included.&lt;/p&gt;




&lt;h2&gt;
  
  
  🔑 1. Core JavaScript &amp;amp; Browser Fundamentals
&lt;/h2&gt;

&lt;p&gt;This is the foundation of every frontend interview. Expect in-depth questions here.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Closures, hoisting, event loop, promises, async/await&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;this&lt;/code&gt;, prototype chain, classes, modules&lt;/li&gt;
&lt;li&gt;Debounce, throttle, memoization, polyfills (implementations)&lt;/li&gt;
&lt;li&gt;Deep vs shallow copy, immutability&lt;/li&gt;
&lt;li&gt;DOM manipulation, shadow DOM, virtual DOM concepts&lt;/li&gt;
&lt;li&gt;Event delegation, bubbling vs capturing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;📚 &lt;strong&gt;Resources&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://javascript.info/" rel="noopener noreferrer"&gt;JavaScript.info&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;You Don’t Know JS&lt;/em&gt; (Kyle Simpson)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ⚛️ 2. React (or Your Main Framework)
&lt;/h2&gt;

&lt;p&gt;At 3 years exp, you need to know React beyond the basics.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hooks in depth (&lt;code&gt;useMemo&lt;/code&gt;, &lt;code&gt;useCallback&lt;/code&gt;, &lt;code&gt;useReducer&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Context API vs Redux vs Zustand (state management tradeoffs)&lt;/li&gt;
&lt;li&gt;Suspense and concurrent rendering&lt;/li&gt;
&lt;li&gt;Controlled vs uncontrolled components&lt;/li&gt;
&lt;li&gt;Performance optimization (lazy loading, code splitting, React Profiler)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;📚 &lt;strong&gt;Resources&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://react.dev" rel="noopener noreferrer"&gt;React.dev&lt;/a&gt; (new docs are amazing)&lt;/li&gt;
&lt;li&gt;EpicReact.dev (Kent C. Dodds, premium but worth it)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🏗️ 3. Frontend System Design
&lt;/h2&gt;

&lt;p&gt;You’ll likely get design-type problems like &lt;em&gt;“build a scalable dashboard”&lt;/em&gt; or &lt;em&gt;“design a search component with autocomplete.”&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Component design patterns (container/presentational, compound components)&lt;/li&gt;
&lt;li&gt;State management strategies in large apps&lt;/li&gt;
&lt;li&gt;Microfrontends &amp;amp; module federation (high-level)&lt;/li&gt;
&lt;li&gt;Caching, pagination, infinite scroll patterns&lt;/li&gt;
&lt;li&gt;Performance budgets (bundle splitting, lazy loading, caching strategies)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;📚 &lt;strong&gt;Resources&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/yangshun/tech-interview-handbook/tree/master/frontend" rel="noopener noreferrer"&gt;Frontend System Design Guide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Grokking Frontend System Design (Educative)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🎨 4. Styling, UI &amp;amp; Accessibility
&lt;/h2&gt;

&lt;p&gt;Interviewers often test practical CSS and accessibility knowledge.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Flexbox &amp;amp; CSS Grid deeply&lt;/li&gt;
&lt;li&gt;Positioning, stacking context, z-index&lt;/li&gt;
&lt;li&gt;Responsive design techniques&lt;/li&gt;
&lt;li&gt;Accessibility (ARIA roles, keyboard navigation, screen reader support)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;📚 &lt;strong&gt;Resources&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://css-tricks.com/" rel="noopener noreferrer"&gt;CSS Tricks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Kevin Powell’s YouTube channel&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ⚡ 5. Performance &amp;amp; Web Internals
&lt;/h2&gt;

&lt;p&gt;They want to know if you can keep apps fast.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Repaint vs reflow&lt;/li&gt;
&lt;li&gt;Lighthouse metrics (FCP, LCP, CLS, TBT)&lt;/li&gt;
&lt;li&gt;Image optimization, code splitting, caching&lt;/li&gt;
&lt;li&gt;Browser storage (localStorage, sessionStorage, IndexedDB)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;📚 &lt;strong&gt;Resources&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://web.dev/learn/" rel="noopener noreferrer"&gt;Web.dev Learn&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  💻 6. Practical Coding Rounds
&lt;/h2&gt;

&lt;p&gt;Beyond DSA, you’ll get &lt;strong&gt;real-world UI problems&lt;/strong&gt;. Some common ones:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Build a Todo app with filtering &amp;amp; persistence&lt;/li&gt;
&lt;li&gt;Create a table with sorting, pagination, infinite scroll&lt;/li&gt;
&lt;li&gt;Implement a debounced search box with API calls&lt;/li&gt;
&lt;li&gt;Write a custom React hook&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 &lt;strong&gt;Tip:&lt;/strong&gt; Practice small projects instead of only solving LeetCode.&lt;/p&gt;




&lt;h2&gt;
  
  
  👥 7. Behavioral &amp;amp; Team Skills
&lt;/h2&gt;

&lt;p&gt;At 3 years, interviewers also want to see maturity:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ownership: &lt;em&gt;“Tell me about a tricky UI bug you solved.”&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Tradeoffs: &lt;em&gt;“Why Redux over Context?”&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Collaboration: working with backend and product teams&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🛠 Suggested Roadmap
&lt;/h2&gt;

&lt;p&gt;Here’s how you can structure your prep:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Revise JS deeply&lt;/strong&gt; (2–3 weeks)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Master React &amp;amp; ecosystem&lt;/strong&gt; (2–3 weeks)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Do 4–5 small projects&lt;/strong&gt; (table, dashboard, search box, etc.)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Learn frontend system design&lt;/strong&gt; (mock design problems)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Mix DSA with UI coding tasks&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;




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

&lt;p&gt;LeetCode clears the initial bar, but &lt;strong&gt;your frontend expertise is what sets you apart.&lt;/strong&gt; Companies want to see if you can build real, scalable, and performant apps—not just reverse a linked list.&lt;/p&gt;

&lt;p&gt;So focus on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;JavaScript + React fundamentals&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Frontend system design&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;UI building &amp;amp; optimization&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That’s how you’ll stand out in a &lt;strong&gt;3 years frontend interview&lt;/strong&gt;.&lt;/p&gt;




</description>
      <category>interview</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Leetcode - 108. Convert Sorted Array to Binary Search Tree</title>
      <dc:creator>Rakesh Reddy Peddamallu</dc:creator>
      <pubDate>Sun, 31 Aug 2025 18:00:27 +0000</pubDate>
      <link>https://forem.com/rakeshreddy512/leetcode-108-convert-sorted-array-to-binary-search-tree-4275</link>
      <guid>https://forem.com/rakeshreddy512/leetcode-108-convert-sorted-array-to-binary-search-tree-4275</guid>
      <description>&lt;p&gt;When you’re given a &lt;strong&gt;sorted array&lt;/strong&gt;, you might be asked to convert it into a &lt;strong&gt;height-balanced binary search tree (BST)&lt;/strong&gt;. A BST is called &lt;em&gt;height-balanced&lt;/em&gt; if the depth of the two subtrees of every node never differs by more than one.&lt;/p&gt;

&lt;p&gt;This problem comes up often in interviews because it beautifully combines recursion, binary search thinking, and tree construction.&lt;/p&gt;




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

&lt;p&gt;Since the array is &lt;strong&gt;sorted&lt;/strong&gt;, the &lt;strong&gt;middle element&lt;/strong&gt; naturally makes the best root:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Everything to the &lt;strong&gt;left&lt;/strong&gt; of the middle is smaller → belongs to the left subtree.&lt;/li&gt;
&lt;li&gt;Everything to the &lt;strong&gt;right&lt;/strong&gt; of the middle is larger → belongs to the right subtree.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By always picking the middle element as the root (and recursively repeating this for subarrays), we ensure the tree stays as &lt;strong&gt;balanced&lt;/strong&gt; as possible.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nums = [-10, -3, 0, 5, 9]

Step 1: Pick middle → 0 (root)
            0
           / \
Step 2:  -10   5
             \    \
Step 3:      -3    9
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This results in a height-balanced BST.&lt;/p&gt;




&lt;h2&gt;
  
  
  🛠️ Approach
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Use &lt;strong&gt;recursion&lt;/strong&gt; with two pointers (&lt;code&gt;l&lt;/code&gt; and &lt;code&gt;r&lt;/code&gt;) to define the current subarray.&lt;/li&gt;
&lt;li&gt;Base case: if &lt;code&gt;l &amp;gt; r&lt;/code&gt;, return &lt;code&gt;null&lt;/code&gt; (no tree to build).&lt;/li&gt;
&lt;li&gt;Find the middle index:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;m&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;l&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Create a new &lt;code&gt;TreeNode&lt;/code&gt; with &lt;code&gt;nums[m]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Recursively build:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Left subtree → &lt;code&gt;helper(l, m - 1)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Right subtree → &lt;code&gt;helper(m + 1, r)&lt;/code&gt;

&lt;ol&gt;
&lt;li&gt;Return the node.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  📜 Code (JavaScript)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */&lt;/span&gt;
&lt;span class="cm"&gt;/**
 * @param {number[]} nums
 * @return {TreeNode}
 */&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;sortedArrayToBST&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;helper&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;l&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;l&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;m&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;l&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;TreeNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;m&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

        &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;helper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;l&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;m&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;helper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;m&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;helper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  ⏱️ Complexity Analysis
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Time Complexity:&lt;/strong&gt; &lt;code&gt;O(n)&lt;/code&gt;&lt;br&gt;
Each element in the array becomes a node in the tree exactly once.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Space Complexity:&lt;/strong&gt; &lt;code&gt;O(log n)&lt;/code&gt; (average case, due to recursion stack depth)&lt;br&gt;
Since the tree is balanced, the maximum recursive calls go as deep as the height of the tree, which is &lt;code&gt;log n&lt;/code&gt;.&lt;br&gt;
In the worst case (if array is skewed or large recursion overhead), it can be &lt;code&gt;O(n)&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ✨ Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Sorted array + middle element logic → perfectly balanced BST.&lt;/li&gt;
&lt;li&gt;Divide-and-conquer strategy keeps the tree height minimal.&lt;/li&gt;
&lt;li&gt;This is a classic recursion problem that also reinforces binary search thinking.&lt;/li&gt;
&lt;/ul&gt;




</description>
      <category>leetcode</category>
    </item>
    <item>
      <title>Leetcode -79. Word Search</title>
      <dc:creator>Rakesh Reddy Peddamallu</dc:creator>
      <pubDate>Sun, 31 Aug 2025 12:33:30 +0000</pubDate>
      <link>https://forem.com/rakeshreddy512/leetcode-79-word-search-33mk</link>
      <guid>https://forem.com/rakeshreddy512/leetcode-79-word-search-33mk</guid>
      <description>&lt;p&gt;The &lt;strong&gt;Word Search&lt;/strong&gt; problem is a classic interview question that tests recursion, grid traversal, and backtracking. In this blog, we’ll solve it two ways:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Using a &lt;code&gt;Set&lt;/code&gt; to track visited cells (clear and beginner-friendly).&lt;/li&gt;
&lt;li&gt;Using an in-place optimization (cleaner and space-efficient).&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  📌 Problem Statement
&lt;/h2&gt;

&lt;p&gt;We are given a 2D board of characters and a word. We need to check if the word exists in the board.&lt;/p&gt;

&lt;p&gt;Rules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The word can be constructed from letters of &lt;strong&gt;adjacent cells&lt;/strong&gt; (up, down, left, right).&lt;/li&gt;
&lt;li&gt;A cell cannot be used more than once in the same path.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Board:
A B C E
S F C S
A D E E

Word: "ABCCED"
Output: true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  💡 Approach 1: Using a Set
&lt;/h2&gt;

&lt;p&gt;The idea is simple:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Try every cell &lt;code&gt;(r, c)&lt;/code&gt; as a potential starting point.&lt;/li&gt;
&lt;li&gt;Use DFS to explore neighbors.&lt;/li&gt;
&lt;li&gt;Keep a &lt;code&gt;Set&lt;/code&gt; of visited cells (so we don’t revisit them).&lt;/li&gt;
&lt;li&gt;Backtrack: after exploring, remove the cell from the &lt;code&gt;Set&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  ✅ Code (with Set)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;exist&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;board&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;rows&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;board&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;cols&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;board&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;pathSet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;backtrack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;rows&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;cols&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
            &lt;span class="nx"&gt;board&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
            &lt;span class="nx"&gt;pathSet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="nx"&gt;pathSet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;backtrack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
                  &lt;span class="nf"&gt;backtrack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
                  &lt;span class="nf"&gt;backtrack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
                  &lt;span class="nf"&gt;backtrack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="nx"&gt;pathSet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// backtrack cleanup&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;cols&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;backtrack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  ⚙️ Complexity (Set version)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Time Complexity:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Worst case: &lt;code&gt;O(rows × cols × 4^L)&lt;/code&gt; where &lt;code&gt;L&lt;/code&gt; = length of the word.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Space Complexity:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;Set&lt;/code&gt; stores up to &lt;code&gt;L&lt;/code&gt; cells.&lt;/li&gt;
&lt;li&gt;Recursion depth = &lt;code&gt;L&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;So &lt;strong&gt;O(L)&lt;/strong&gt; extra space.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;This is intuitive and great for understanding recursion, but we can do better.&lt;/p&gt;




&lt;h2&gt;
  
  
  💡 Approach 2: Optimized In-Place Marking
&lt;/h2&gt;

&lt;p&gt;Instead of maintaining a &lt;code&gt;Set&lt;/code&gt;, we can &lt;strong&gt;modify the board in-place&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;At each step, temporarily set the current cell to &lt;code&gt;"#"&lt;/code&gt; to mark it visited.&lt;/li&gt;
&lt;li&gt;Explore neighbors.&lt;/li&gt;
&lt;li&gt;Restore the original character when backtracking.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This way, “visited check” happens naturally because a &lt;code&gt;"#"&lt;/code&gt; will never match the next character in &lt;code&gt;word&lt;/code&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  ✅ Code (in-place optimization)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;exist&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;board&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;rows&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;board&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;cols&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;board&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;backtrack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;rows&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;cols&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
            &lt;span class="nx"&gt;board&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// Mark visited by modifying the board&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;board&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="nx"&gt;board&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;backtrack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
                  &lt;span class="nf"&gt;backtrack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
                  &lt;span class="nf"&gt;backtrack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
                  &lt;span class="nf"&gt;backtrack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Restore for other paths&lt;/span&gt;
        &lt;span class="nx"&gt;board&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;cols&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;backtrack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  ⚙️ Complexity (Optimized version)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity:&lt;/strong&gt;
Still &lt;code&gt;O(rows × cols × 4^L)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity:&lt;/strong&gt;
Only recursion depth → &lt;strong&gt;O(L)&lt;/strong&gt;.
No &lt;code&gt;Set&lt;/code&gt;, no extra storage. Cleaner and faster.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔑 Comparison
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;With Set&lt;/th&gt;
&lt;th&gt;In-place Optimization&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Clarity&lt;/td&gt;
&lt;td&gt;✅ Easy to follow&lt;/td&gt;
&lt;td&gt;⚡ Cleaner, shorter&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Extra Space&lt;/td&gt;
&lt;td&gt;O(L) (Set)&lt;/td&gt;
&lt;td&gt;O(1) extra&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Speed&lt;/td&gt;
&lt;td&gt;Slightly slower&lt;/td&gt;
&lt;td&gt;Faster in practice&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Interview value&lt;/td&gt;
&lt;td&gt;Good starter&lt;/td&gt;
&lt;td&gt;Impressive finish&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  ✨ Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Set approach&lt;/strong&gt;: Great for beginners, shows clear backtracking logic.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;In-place approach&lt;/strong&gt;: Professional, space-efficient, and elegant.&lt;/li&gt;
&lt;li&gt;Both methods teach the importance of &lt;strong&gt;backtracking cleanup&lt;/strong&gt; — the trick that makes recursion work correctly.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;👉 In an &lt;strong&gt;interview&lt;/strong&gt;, I’d start with the Set approach to explain my thought process, then optimize to the in-place version to show deeper problem-solving skills.&lt;/p&gt;




</description>
      <category>leetcode</category>
    </item>
    <item>
      <title>22. Generate Parentheses</title>
      <dc:creator>Rakesh Reddy Peddamallu</dc:creator>
      <pubDate>Sun, 31 Aug 2025 04:48:52 +0000</pubDate>
      <link>https://forem.com/rakeshreddy512/22-generate-parentheses-4pbc</link>
      <guid>https://forem.com/rakeshreddy512/22-generate-parentheses-4pbc</guid>
      <description>&lt;p&gt;Got it, Rakesh 🚀 Let’s turn this into a neat blog post. I’ll explain the problem, walk through the approach, give a complexity analysis, and finally embed your visualization iframe.&lt;/p&gt;

&lt;p&gt;Here’s a polished draft for your blog:&lt;/p&gt;




&lt;h1&gt;
  
  
  Generate Parentheses — Explained with Backtracking &amp;amp; Visualization
&lt;/h1&gt;

&lt;p&gt;One of the classic problems in coding interviews (especially LeetCode #22) is &lt;strong&gt;"Generate Parentheses"&lt;/strong&gt;. It’s a great way to understand &lt;strong&gt;backtracking&lt;/strong&gt;, recursion, and how constraints guide the search space.&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Given &lt;code&gt;n&lt;/code&gt; pairs of parentheses, generate &lt;strong&gt;all possible combinations&lt;/strong&gt; of well-formed parentheses.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="nx"&gt;Output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;((()))&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;(()())&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;(())()&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;()(())&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;()()()&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Approach
&lt;/h2&gt;

&lt;p&gt;This problem is a textbook case for &lt;strong&gt;backtracking&lt;/strong&gt;.&lt;br&gt;
We want to build strings step by step, only adding characters when they keep the parentheses &lt;strong&gt;valid&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Rules for Valid Parentheses:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;We can add an opening parenthesis &lt;code&gt;"("&lt;/code&gt; as long as we haven’t used up all &lt;code&gt;n&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We can add a closing parenthesis &lt;code&gt;")"&lt;/code&gt; &lt;strong&gt;only if&lt;/strong&gt; the number of closing brackets is less than the number of opening ones so far (to avoid invalid strings like &lt;code&gt;")("&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We use a &lt;strong&gt;stack&lt;/strong&gt; to build the current sequence and a recursive function to explore all possibilities.&lt;/p&gt;


&lt;h2&gt;
  
  
  Code Implementation
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * @param {number} n
 * @return {string[]}
 */&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;generateParenthesis&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;backtrack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;openN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;closedN&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Base case: if both counts reach n, we found a valid string&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;openN&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;closedN&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// Add "(" if we still have openings left&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;openN&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nf"&gt;backtrack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;openN&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;closedN&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// backtrack&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// Add ")" if it's valid&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;openN&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;closedN&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;)&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nf"&gt;backtrack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;openN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;closedN&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// backtrack&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="nf"&gt;backtrack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Complexity Analysis
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity:&lt;/strong&gt;
Each valid sequence has length &lt;code&gt;2n&lt;/code&gt;. The number of valid sequences is the &lt;strong&gt;nth Catalan number&lt;/strong&gt;:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;$$&lt;br&gt;
  C_n = \frac{1}{n+1} \binom{2n}{n}&lt;br&gt;
  $$&lt;/p&gt;

&lt;p&gt;So, the complexity is &lt;strong&gt;O(Cn × n)&lt;/strong&gt; (since joining each sequence takes &lt;code&gt;O(n)&lt;/code&gt; time).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Space Complexity:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The recursion depth is at most &lt;code&gt;2n&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We use a stack of size &lt;code&gt;2n&lt;/code&gt; in the worst case.&lt;/li&gt;
&lt;li&gt;Result storage takes &lt;code&gt;O(Cn × n)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So overall space complexity is &lt;strong&gt;O(Cn × n)&lt;/strong&gt;.&lt;/p&gt;


&lt;h2&gt;
  
  
  Visualization 🎨
&lt;/h2&gt;

&lt;p&gt;Here’s an interactive visualization of the backtracking tree:&lt;br&gt;
&lt;iframe src="https://codesandbox.io/embed/mpl8f6"&gt;
&lt;/iframe&gt;
&lt;/p&gt;




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

&lt;p&gt;This problem is a perfect introduction to &lt;strong&gt;backtracking&lt;/strong&gt;. It teaches how to explore a search space while pruning invalid paths early. Once you master this, you’ll notice a similar structure in problems like &lt;strong&gt;N-Queens&lt;/strong&gt;, &lt;strong&gt;Combinations&lt;/strong&gt;, and &lt;strong&gt;Subsets&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;👉 Tip: When stuck in a backtracking problem, always ask:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;“What choices do I have at this step?”&lt;/li&gt;
&lt;li&gt;“What constraints stop me from making a choice?”&lt;/li&gt;
&lt;/ul&gt;




</description>
      <category>leetcode</category>
    </item>
    <item>
      <title>Leetcode - 52. N-Queens II</title>
      <dc:creator>Rakesh Reddy Peddamallu</dc:creator>
      <pubDate>Sat, 30 Aug 2025 13:00:15 +0000</pubDate>
      <link>https://forem.com/rakeshreddy512/leetcode-52-n-queens-ii-2m7k</link>
      <guid>https://forem.com/rakeshreddy512/leetcode-52-n-queens-ii-2m7k</guid>
      <description>&lt;h1&gt;
  
  
  🏰 Solving the N-Queens Problem with Backtracking
&lt;/h1&gt;

&lt;p&gt;The &lt;strong&gt;N-Queens problem&lt;/strong&gt; is a classic in algorithms and interview prep. It asks:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Place &lt;strong&gt;N queens&lt;/strong&gt; on an &lt;strong&gt;N×N chessboard&lt;/strong&gt; so that no two queens attack each other.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Queens can attack vertically, horizontally, and diagonally. The task is to count the total number of valid arrangements.&lt;/p&gt;




&lt;h2&gt;
  
  
  ✨ Key Insight
&lt;/h2&gt;

&lt;p&gt;When placing queens row by row:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each &lt;strong&gt;row&lt;/strong&gt; can have only one queen.&lt;/li&gt;
&lt;li&gt;Each &lt;strong&gt;column&lt;/strong&gt; can have only one queen.&lt;/li&gt;
&lt;li&gt;Each &lt;strong&gt;diagonal&lt;/strong&gt; can have only one queen.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, at each step, we just need to check if placing a queen in the current cell &lt;code&gt;(r, c)&lt;/code&gt; is safe. If yes → place it and recurse to the next row.&lt;/p&gt;




&lt;h2&gt;
  
  
  ⚙️ Backtracking Approach
&lt;/h2&gt;

&lt;p&gt;We use recursion to try all possible placements:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Row by row recursion&lt;/strong&gt; – Start from row &lt;code&gt;0&lt;/code&gt;, and for each column, check if placing a queen is valid.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use sets for quick lookup&lt;/strong&gt;:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;col&lt;/code&gt;: columns where queens are already placed.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;posDiag&lt;/code&gt;: positive diagonals (&lt;code&gt;r + c&lt;/code&gt; is constant).&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;negDiag&lt;/code&gt;: negative diagonals (&lt;code&gt;r - c&lt;/code&gt; is constant).&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If a position &lt;code&gt;(r, c)&lt;/code&gt; is safe:&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Place queen → add to sets.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Recurse for next row.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Backtrack → remove from sets.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;When &lt;code&gt;r === n&lt;/code&gt;, we found a valid arrangement → increment result.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🖥️ Implementation (JavaScript)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * @param {number} n
 * @return {number}
 */&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;totalNQueens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;col&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;posDiag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;negDiag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;backtrack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// Found a valid board&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// Check if queen can be placed&lt;/span&gt;
            &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;col&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;posDiag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;negDiag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="c1"&gt;// Place queen&lt;/span&gt;
            &lt;span class="nx"&gt;col&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nx"&gt;posDiag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nx"&gt;negDiag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="c1"&gt;// Recurse to next row&lt;/span&gt;
            &lt;span class="nf"&gt;backtrack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="c1"&gt;// Backtrack (remove queen)&lt;/span&gt;
            &lt;span class="nx"&gt;col&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nx"&gt;posDiag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nx"&gt;negDiag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="nf"&gt;backtrack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  ⏱️ Time &amp;amp; Space Complexity
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Time Complexity:&lt;/strong&gt; &lt;code&gt;O(N!)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the worst case, we try &lt;code&gt;N&lt;/code&gt; options for row 1, &lt;code&gt;N-1&lt;/code&gt; for row 2, and so on.&lt;/li&gt;
&lt;li&gt;The actual branching is smaller due to pruning (invalid placements), but &lt;code&gt;O(N!)&lt;/code&gt; is a safe upper bound.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Space Complexity:&lt;/strong&gt; &lt;code&gt;O(N)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sets &lt;code&gt;col&lt;/code&gt;, &lt;code&gt;posDiag&lt;/code&gt;, and &lt;code&gt;negDiag&lt;/code&gt; hold at most &lt;code&gt;N&lt;/code&gt; elements.&lt;/li&gt;
&lt;li&gt;Recursion stack depth is &lt;code&gt;N&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  ✅ Example Run
&lt;/h2&gt;

&lt;p&gt;For &lt;code&gt;n = 4&lt;/code&gt;, the valid solutions are 2:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;The function correctly returns &lt;code&gt;2&lt;/code&gt;.&lt;/p&gt;




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

&lt;ul&gt;
&lt;li&gt;This problem is a &lt;strong&gt;perfect introduction to backtracking&lt;/strong&gt; because you learn how to try, prune, and backtrack.&lt;/li&gt;
&lt;li&gt;Sets make the lookup &lt;strong&gt;O(1)&lt;/strong&gt;, which is much cleaner than scanning the board every time.&lt;/li&gt;
&lt;li&gt;Once you master this, you can extend it to actually &lt;strong&gt;return the board configurations&lt;/strong&gt; instead of just counting them.&lt;/li&gt;
&lt;/ul&gt;




</description>
      <category>leetcode</category>
    </item>
    <item>
      <title>Leetcode - 39. Combination Sum</title>
      <dc:creator>Rakesh Reddy Peddamallu</dc:creator>
      <pubDate>Sat, 30 Aug 2025 09:31:41 +0000</pubDate>
      <link>https://forem.com/rakeshreddy512/leetcode-39-combination-sum-598j</link>
      <guid>https://forem.com/rakeshreddy512/leetcode-39-combination-sum-598j</guid>
      <description>&lt;p&gt;The &lt;strong&gt;Combination Sum&lt;/strong&gt; problem is a classic backtracking question from LeetCode (#39).&lt;/p&gt;

&lt;p&gt;We are given:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An array of positive integers &lt;code&gt;candidates&lt;/code&gt; (no duplicates inside).&lt;/li&gt;
&lt;li&gt;A target integer &lt;code&gt;target&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We must return &lt;strong&gt;all unique combinations&lt;/strong&gt; where the chosen numbers add up exactly to &lt;code&gt;target&lt;/code&gt;.&lt;br&gt;
Numbers can be reused unlimited times.&lt;/p&gt;


&lt;h2&gt;
  
  
  🎯 Example
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;candidates&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;[2, 2, 3]&lt;/code&gt; → adds to 7&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[7]&lt;/code&gt; → also adds to 7&lt;/li&gt;
&lt;li&gt;No other valid combinations exist.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🧠 Approach – Backtracking (DFS)
&lt;/h2&gt;

&lt;p&gt;This is a &lt;strong&gt;search problem&lt;/strong&gt;. Think of it like exploring all possible "paths" of numbers until:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The sum equals &lt;code&gt;target&lt;/code&gt; → ✅ valid combination&lt;/li&gt;
&lt;li&gt;The sum exceeds &lt;code&gt;target&lt;/code&gt; → ❌ invalid, stop&lt;/li&gt;
&lt;li&gt;We’ve tried all numbers → ❌ stop&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We use &lt;strong&gt;DFS + backtracking&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Start at index &lt;code&gt;0&lt;/code&gt; with an empty path &lt;code&gt;[]&lt;/code&gt; and total &lt;code&gt;0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;At each step:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Either &lt;strong&gt;include&lt;/strong&gt; the current candidate (stay on same index since we can reuse it).&lt;/li&gt;
&lt;li&gt;Or &lt;strong&gt;skip&lt;/strong&gt; it and move to the next index.

&lt;ol&gt;
&lt;li&gt;Whenever &lt;code&gt;total === target&lt;/code&gt;, we push a copy of the path into results.&lt;/li&gt;
&lt;li&gt;Backtrack by popping out the last choice and continue exploring.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ✅ Code (JavaScript)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * @param {number[]} candidates
 * @param {number} target
 * @return {number[][]}
 */&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;combinationSum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;candidates&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dfs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;([...&lt;/span&gt;&lt;span class="nx"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// store valid combination&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;candidates&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// stop exploring&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// 1. Choose the current number&lt;/span&gt;
        &lt;span class="nx"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;candidates&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
        &lt;span class="nf"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;candidates&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// stay on i since reuse is allowed&lt;/span&gt;
        &lt;span class="nx"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// backtrack&lt;/span&gt;

        &lt;span class="c1"&gt;// 2. Skip the current number&lt;/span&gt;
        &lt;span class="nf"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="nf"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  ⏱️ Time and Space Complexity
&lt;/h2&gt;

&lt;p&gt;Let’s analyze:&lt;/p&gt;

&lt;h3&gt;
  
  
  Time Complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;In the worst case, each number can be chosen multiple times until we exceed the target.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;height of the recursion tree&lt;/strong&gt; is at most &lt;code&gt;target / min(candidates)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;At each level, we branch into &lt;strong&gt;choose or skip&lt;/strong&gt; → about &lt;code&gt;2^n&lt;/code&gt; style growth.&lt;/li&gt;
&lt;li&gt;Worst-case complexity:
&lt;strong&gt;O(2^n * t)&lt;/strong&gt;
where &lt;code&gt;t&lt;/code&gt; is the target (because we keep adding numbers until reaching it).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In practice, it runs much faster due to pruning (&lt;code&gt;total &amp;gt; target&lt;/code&gt;).&lt;/p&gt;

&lt;h3&gt;
  
  
  Space Complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;O(target/min(candidates))&lt;/strong&gt; for the recursion depth (stack space).&lt;/li&gt;
&lt;li&gt;Plus storage for results, which could be exponential in the number of combinations.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🚀 Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Backtracking is perfect when we need &lt;strong&gt;all possible solutions&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The trick is in &lt;strong&gt;branching&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reuse the current candidate → &lt;code&gt;dfs(i, ...)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Skip it → &lt;code&gt;dfs(i+1, ...)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;The stopping condition &lt;code&gt;if (i &amp;gt;= candidates.length || total &amp;gt; target)&lt;/code&gt; prevents infinite recursion.&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;




</description>
      <category>leetcode</category>
    </item>
  </channel>
</rss>
