<?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 Ivan Shirokov on Medium]]></title>
        <description><![CDATA[Stories by Ivan Shirokov on Medium]]></description>
        <link>https://medium.com/@ivanshirokoff_35477?source=rss-5aac68cc27df------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*k5Cqu4Pj-OcnPTzq</url>
            <title>Stories by Ivan Shirokov on Medium</title>
            <link>https://medium.com/@ivanshirokoff_35477?source=rss-5aac68cc27df------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 14:49:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ivanshirokoff_35477/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[Coding interview problem 2-SUM explained]]></title>
            <link>https://medium.com/@ivanshirokoff_35477/coding-interview-problem-2-sum-explained-38462eaddc71?source=rss-5aac68cc27df------2</link>
            <guid isPermaLink="false">https://medium.com/p/38462eaddc71</guid>
            <category><![CDATA[coding-interviews]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[interview]]></category>
            <dc:creator><![CDATA[Ivan Shirokov]]></dc:creator>
            <pubDate>Mon, 02 Sep 2019 20:17:52 GMT</pubDate>
            <atom:updated>2019-09-02T20:53:39.890Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jBgcK5sLhAdi6x85PKl02w.jpeg" /></figure><p>I’m going to explain how to approach one of the most popular coding problems that one may run into on a phone interview to Google or Facebook.</p><p>So here&#39;s the problem: you’re given an array of numbers and you need to answer if there’re two numbers in that array that sum up to a specific target.</p><p>For example, for the array [1, 4, 7, -3, 2] and a target of 9 the answer is yes. Cause 7 + 2 == 9.</p><p>Now, there’re a couple of ways you can tackle the problem.</p><h3>Brute force</h3><p>Of course, you could find all possible pairs of numbers and check if one of them sum up to our target. The complexity of this algorithm would be O(n²).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z6ktuL0yWK-1-fRASMumXg.png" /><figcaption>Example of code in Python3</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/446/1*fEn__M4ec-Eafypuz-2TDQ.gif" /><figcaption>Algorithm demonstration</figcaption></figure><p>I guess O(n²) is not something guys from a big tech company would expect you to come up with. Luckily we can do much better.</p><h3>Linear time approach</h3><p>The idea is to get rid of scanning through the array on each iteration. You can use a Set to store numbers that you’ve seen before. Now you can in O(1) time look up if there’s a number that sums up with the current number to the given target. Which makes overall complexity of the algorithm O(n).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w6Mcfn_jmkKPv1WIYrUSEw.png" /><figcaption>Example of code in Python3</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/446/1*2vHxW9sNJiKn78BpIIHfoA.gif" /><figcaption>Algorithm demonstration</figcaption></figure><p>There’re various types of that problem. You might be asked to find indices of these two numbers. In that case, you could use a HashMap to store numbers indices.</p><p>That’s it from me on the two-sum problem. See you in the next stories.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=38462eaddc71" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>