<?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 Applied Interview Prep Course on Medium]]></title>
        <description><![CDATA[Stories by Applied Interview Prep Course on Medium]]></description>
        <link>https://medium.com/@interviewprep?source=rss-3de8cc46d255------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Mb5HcbxrQM7iYXHv.jpg</url>
            <title>Stories by Applied Interview Prep Course on Medium</title>
            <link>https://medium.com/@interviewprep?source=rss-3de8cc46d255------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 16:11:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@interviewprep/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[Find Pair in an array with given sum k]]></title>
            <link>https://medium.com/@interviewprep/find-pair-in-an-array-with-given-sum-k-c5bc6e6311b?source=rss-3de8cc46d255------2</link>
            <guid isPermaLink="false">https://medium.com/p/c5bc6e6311b</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[arrays]]></category>
            <category><![CDATA[lists]]></category>
            <category><![CDATA[problem-solving]]></category>
            <dc:creator><![CDATA[Applied Interview Prep Course]]></dc:creator>
            <pubDate>Sun, 08 Sep 2019 13:36:31 GMT</pubDate>
            <atom:updated>2019-09-08T13:36:31.608Z</atom:updated>
            <content:encoded><![CDATA[<p>Given a list of numbers and a number k, return whether any two numbers from the list add up to k. (Recently Asked in Google)</p><p>For example, given [11, 15, 3, 8] and k of 19, return true since 11 + 8is 19.</p><p>This problem can be solved in several different ways.</p><p><strong>Brute force approach:</strong> In this approach we solve using a nested iteration to check for every pair of numbers:</p><p>Python code:</p><pre>def two_sum(lst, k):<br>    for i in range(len(lst)):<br>        for j in range(len(lst)):<br>            if i != j and lst[i] + lst[j] == k:<br>                return True<br>  return False</pre><p>Time complexity: O(n²)</p><p><strong>Best Approach:</strong></p><p>We can solve using a set to remember the numbers we’ve seen so far. Then for a given number, we can check if there is another number that, if added, would sum to k.</p><pre>def two_sum(lst, k):<br>    seen = set()<br>    for num in lst:<br>        if k - num in seen:<br>            return True<br>        seen.add(num)<br>    return False</pre><p><strong>Time complexity: O(n)</strong> since lookup of sets are O(1) each</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c5bc6e6311b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Sort Colors (Dutch national flag sorting problem)]]></title>
            <link>https://medium.com/@interviewprep/sort-colors-dutch-national-flag-sorting-problem-9fb400519d8?source=rss-3de8cc46d255------2</link>
            <guid isPermaLink="false">https://medium.com/p/9fb400519d8</guid>
            <category><![CDATA[arrays]]></category>
            <category><![CDATA[sorting-algorithms]]></category>
            <category><![CDATA[sort-0s-1s-and-2s]]></category>
            <dc:creator><![CDATA[Applied Interview Prep Course]]></dc:creator>
            <pubDate>Sat, 27 Jul 2019 11:36:03 GMT</pubDate>
            <atom:updated>2019-07-28T07:21:15.965Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Problem Statement:</strong> Given an array with <em>n</em> objects colored red, white or blue, sort them <a href="https://en.wikipedia.org/wiki/In-place_algorithm"><strong>in-place</strong></a><strong> </strong>so that objects of the same color are adjacent, with the colors in the order <em>red</em>, <em>white</em> and <em>blue</em>.</p><p>Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.</p><p><strong>Note:</strong> You are not suppose to use the library’s sort function for this problem.</p><p><strong>Simple Solution:(Two pass solution)</strong></p><ol><li>Count the number of 0’s, 1’s and 2’s.</li><li>After Counting, put all 0’s first, then 1’s and lastly 2’s in the array.</li></ol><p><strong>Analysis:</strong></p><p>Time complexity will be O(n).</p><p>Space complexity will be O(1), we didn’t used any other extra space</p><p><strong>Solution 2: (One pass solution)</strong></p><p>Let’s use here three pointers to track the rightmost boundary of zeros, the leftmost boundary of twos and the current element under the consideration.</p><p>Idea of the solution is as follows</p><p>Move <strong>curr</strong> pointer along the array, if the arr[curr] = 0 then swap it with arr[p0] and arr[curr]=2 then swap it with arr[p2]</p><p><strong>Algorithm:</strong></p><ol><li>Create a <strong>p0</strong> pointer at the beginning of the array and a <strong>p2</strong> pointer at the end of the array.</li><li>Create a <strong>curr</strong> pointer that starts at the beginning of the array and iterates through each element.</li><li>If the element at arr[curr] is a 2, then swap arr[curr] and arr[p2] and decrease the <strong>p2</strong> pointer by 1.</li><li>If the element at arr[curr] is a 0, then swap arr[curr] and arr[p0] and increase the <strong>p0</strong> and <strong>curr</strong> pointers by 1.</li><li>If the element at arr[curr] is a 1, don’t swap anything and just increase the <strong>curr</strong> pointer by 1.</li><li>Step 3, step 4 and step 5 will repeat until <strong>curr ≤ p2</strong></li></ol><p><strong>Example:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/663/1*4nmQyqHkA2pdoVfX5aWEhg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/652/1*kqDUamDhf8dgV6jETt1CjQ.png" /></figure><p><strong>Pseudo Code:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*AWp3Qp8h-OVoJW7yZsRcOQ.png" /></figure><p><strong>Analysis:</strong></p><p>Time complexity will be O(n).</p><p>Space complexity will be O(1), we didn’t used any other extra space</p><h4>For more problems please check: <a href="https://interviewprep.appliedcourse.com/?source=post_page---------------------------">https://interviewprep.appliedcourse.com/</a></h4><p>(or)</p><p>Reach out us at +91–844–844–0102</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9fb400519d8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ugly Number (Dynamic Programming)]]></title>
            <link>https://medium.com/@interviewprep/ugly-number-dynamic-programming-94520110a084?source=rss-3de8cc46d255------2</link>
            <guid isPermaLink="false">https://medium.com/p/94520110a084</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[ugly-number]]></category>
            <category><![CDATA[dynamic-programming]]></category>
            <category><![CDATA[data-structures]]></category>
            <dc:creator><![CDATA[Applied Interview Prep Course]]></dc:creator>
            <pubDate>Tue, 23 Jul 2019 12:44:47 GMT</pubDate>
            <atom:updated>2019-07-23T12:44:47.213Z</atom:updated>
            <content:encoded><![CDATA[<h3>Write a program to find the n-th ugly number.</h3><p><strong>Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. </strong>Note that 1 is typically treated as an ugly number.</p><p><strong>Example:</strong> 10th ugly number is 12, because first 10 ugly numbers are 1, 2, 3, 4, 5, 6, 8, 9, 10, 12.</p><h4><strong>One of the Approach to solve the problem:</strong></h4><p>If we carefully observe the example, We can see that an ugly number is a multiple of either 2, 3 or 5. Only exception is that 1 is also ugly.</p><p><strong>Question:</strong> Starting from 1 can we multiply 1 by 2, 3, and 5 then can we get next 3 ugly numbers {2, 3, 5}?</p><p><strong>Answer:</strong> No, because there is one more ugly number 4 before 5. So it is obvious that each time we pick minimum ugly and compute all the numbers multiples of 2,3, and 5 of that number and add it to a data structure. Next time we pick the minimum and so on.</p><p><strong>Question:</strong> <em>Which data structures fit where we always get min in O(1) time?</em></p><p><strong>Answer:</strong> MinHeap</p><p>Solution is as follows:</p><p>we will keep a min heap and initially insert 1 as the first ugly number. The heap is {1}.</p><p>Now, for next ugly number <strong>we extract min 1 from heap</strong> and inserts the multiples of 2, 3, and 5. Now, the heap becomes {2, 3, 5}.</p><p>For next ugly number, we extract min from heap which is 2. SO, Now, we insert 2*2=4, 2*3=6, 2*5=10 into the heap and the heap becomes {3, 4, 5, 6, 10}.</p><p>Next we extract 3 and push 3*2=6, 3*3=9, and 3*5=15. Note that, we already inserted 6 so we should not push another 6 into heap. Now heap becomes {4, 5, 6, 9, 10, 15}. And so on.</p><p><strong>Question:</strong> <em>What is the time and space complexity?</em></p><p><strong>Answer: </strong>We extract one element at each step until we extract <strong>nth</strong> element to get the nth ugly. Each extract operation we need to do a heapify operation to maintain heap property which take O(logn) on average. So, total complexity is <strong>O(nlogn)</strong>.</p><h4>Next Question: Can we improve the time complexity?</h4><p>Approach 2: <strong>Dynamic Programming</strong></p><p>In the previous approach, we are computing many duplicate ugly numbers. <strong>For example: 6 </strong>can be generated as 3*2 or 2*3. So, we could potentially improve by caching the computation is DP table.</p><blockquote><strong>How?</strong></blockquote><pre>1*2, 2*2, 3*2, 4*2, ...<br>1*3, 2*3, 3*3, 4*3, ...<br>1*5, 2*5, 3*5, 4*5, ...</pre><pre>we take the elements in order of -<br>1*2, 1*3, 2*2, 1*5, 2*3, 3*3, 2*5, ...</pre><p>If we look closely then we will see that this is a simple merge procedure we do in Merge Sort. It’s 3-way merge.</p><p>The algorithm is straightforward: choose the smallest ugly number among 2×arr[p2​], 3×arr[p3​], and 5×arr[p5​] and add it into the array. Move the corresponding pointer by one step. Repeat till you’ll have <strong>n</strong> ugly numbers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uQl2uxnGSBNhLb79U1yvbQ.jpeg" /></figure><p><strong>Algorithm:</strong></p><ol><li>Pre compute n ugly numbers:</li></ol><blockquote>Initiate array of precomputed ugly numbers <strong>arr</strong> and three pointers p2, p3 and p5 to track the index of the last ugly number used to produce the next ones.</blockquote><blockquote>Make a loop of n steps. At each step:</blockquote><blockquote>Choose the smallest number among arr[p2] * 2, arr[p3] * 3, and arr[p5] * 5 and add it into arr.</blockquote><blockquote>Move by one the pointer which corresponds to the “ancestor” of the added number.</blockquote><p>2. Retrieve needed ugly number from the array of precomputed numbers.</p><p><strong>C- Code: </strong><a href="https://ideone.com/3ZEX8Q">https://ideone.com/3ZEX8Q</a></p><h3>Detailed Video Explanation:</h3><p><a href="https://interviewprep.appliedcourse.com/lecture/2/interview-preparation-course/496/nth-ugly-number/0/free-videos">https://interviewprep.appliedcourse.com/lecture/2/interview-preparation-course/496/nth-ugly-number/0/free-videos</a></p><p>For more problems check: <a href="https://interviewprep.appliedcourse.com/">https://interviewprep.appliedcourse.com/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=94520110a084" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>