<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Karolis Krulis on Medium]]></title>
        <description><![CDATA[Stories by Karolis Krulis on Medium]]></description>
        <link>https://medium.com/@karoliskrulis?source=rss-7f05321ab9f5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*BDPY6xhDGo3B_wGNxZYcdA@2x.jpeg</url>
            <title>Stories by Karolis Krulis on Medium</title>
            <link>https://medium.com/@karoliskrulis?source=rss-7f05321ab9f5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 06:48:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@karoliskrulis/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Technical Interview Guide: Solving String Permutations with Recursion]]></title>
            <link>https://medium.com/@karoliskrulis/technical-interview-guide-solving-string-permutations-with-recursion-8c81dde2f62b?source=rss-7f05321ab9f5------2</link>
            <guid isPermaLink="false">https://medium.com/p/8c81dde2f62b</guid>
            <category><![CDATA[technical-interview]]></category>
            <category><![CDATA[interview-questions]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[Karolis Krulis]]></dc:creator>
            <pubDate>Wed, 12 Feb 2025 04:35:16 GMT</pubDate>
            <atom:updated>2025-02-12T04:35:16.365Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*jqlqs-xH-TYFXsgObRN3wQ.gif" /></figure><p>Technical interviews often feature recursion problems, testing how well candidates can break down complex tasks into smaller, manageable parts. One of the most common questions in this category is generating all permutations of a string. This guide will help both <strong>interviewees</strong> prepare a strong solution and <strong>interviewers</strong> understand how to evaluate responses effectively.</p><h3>The Problem</h3><p>Given a string with unique characters, write a function that returns all possible permutations of the string as a set. For example, given the string &quot;dog&quot;, the permutations would be:</p><pre>dog<br>dgo<br>odg<br>gdo<br>god<br>ogd</pre><p>The goal is to solve this using a <strong>recursive approach</strong>.</p><h3>For Interviewees: How to Solve It</h3><h4>Step-by-Step Breakdown — Building the Thought Process</h4><p>Let’s start small and build up. Recursion works best when we break the problem into smaller, similar subproblems and solve those first.</p><p>Start with the simplest case: if the input string has just one character, the answer is straightforward — it’s just that character itself.</p><pre>if (string.length &lt;= 1) return new Set([string]);</pre><p>This is the <strong>base case</strong>. Every recursive solution needs one to avoid infinite loops.</p><p>Now think about a slightly bigger problem. What if we had two characters, like &quot;ab&quot;? The permutations are &quot;ab&quot; and &quot;ba&quot;. You can think of this as taking the first character (&quot;a&quot;) and inserting it in all possible positions of the permutations of &quot;b&quot;.</p><p>What about a longer string like &quot;abc&quot;? First, get all permutations of &quot;ab&quot;: &quot;ab&quot;, &quot;ba&quot;. Then insert &quot;c&quot; into every position in each of those permutations:</p><p>For &quot;ab&quot;: &quot;cab&quot;, &quot;acb&quot;, &quot;abc&quot;</p><p>For &quot;ba&quot;: &quot;cba&quot;, &quot;bca&quot;, &quot;bac&quot;</p><p><strong>We’ve found a pattern! </strong>To get permutations for a string of length n, we:</p><ul><li>Get all permutations of the first n - 1 characters.</li><li>Insert the last character into every possible position in each of those smaller permutations.</li></ul><h3>Code Solution — Putting It All Together</h3><pre>function getPermutations(string) {<br><br>  if (string.length &lt;= 1) {<br>    return new Set([string]);<br>  }<br><br>  const allCharsExceptLast = string.slice(0, -1);<br>  const lastChar = string[string.length - 1];<br>  const permutationsOfAllCharsExceptLast = getPermutations(allCharsExceptLast);<br>  const permutations = new Set();<br>  <br>  permutationsOfAllCharsExceptLast.forEach(permutation =&gt; {<br>    for (let position = 0; position &lt;= allCharsExceptLast.length; position++) {<br>      const newPermutation = permutation.slice(0, position) + lastChar + permutation.slice(position);<br>      permutations.add(newPermutation);<br>    }<br>  });<br><br>  return permutations;<br>}</pre><h3>Common Pitfalls — and How to Avoid Them</h3><p>Missing the base case can cause infinite recursion. Always ensure you reduce the problem size with each recursive call. Use a set to avoid duplicate results.</p><h3>Bonus Tips</h3><p>Be ready to handle strings with duplicate characters. Discuss time complexity: <strong>O(n!)</strong> (factorial time complexity) for generating all permutations. Mention iterative alternatives if asked.</p><h3>For Interviewers: How to Evaluate</h3><h4>What This Question Reveals</h4><p>This problem helps assess recursive thinking: how well a candidate can break down the problem into subproblems and recognize the simplest version of the problem. It also reveals how well they explain their thought process.</p><h4>Follow-Up Questions</h4><ul><li>How would the solution change if the string could contain duplicate characters?</li><li>What’s the time complexity of this solution?</li><li>Can you write an iterative version?</li></ul><h4>Red Flags</h4><ul><li><strong>Overcomplication</strong>: Introducing unnecessary complexity instead of using a straightforward recursive approach.</li><li><strong>Unclear Problem Decomposition</strong>: Jumping into code without explaining how to break the problem into smaller subproblems.</li><li><strong>Inconsistent Handling of Edge Cases</strong>: Missing key cases like empty strings or strings with repeated characters.</li><li><strong>Avoiding Set/Unique Constraints</strong>: Failing to handle duplicates in the result set, indicating a weak understanding of permutations.</li><li><strong>No Discussion of Optimization</strong>: Stopping after a basic solution without considering time complexity improvements or iterative alternatives.</li></ul><h4>Discussion Points</h4><p>Use this opportunity to tie the interview conversation together. Discuss practical ways to reduce memory usage or improve performance in real-world applications. Compare recursive and iterative approaches, highlighting when each one is most appropriate. This can be a great segue into deeper topics, such as algorithm optimization or alternative problem-solving strategies, giving the candidate a chance to show broader understanding.</p><h3>Wrap-Up</h3><p>Mastering recursion takes practice, and string permutations are a great way to build that skill. For interviewees, focus on breaking the problem into subproblems and explaining your process clearly. For interviewers, use this question to assess how candidates think through recursion, handle edge cases, and optimize their solutions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8c81dde2f62b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>