{"id":578,"date":"2020-04-17T03:23:18","date_gmt":"2020-04-17T03:23:18","guid":{"rendered":"http:\/\/markedcode.com\/?p=578"},"modified":"2020-04-27T16:31:36","modified_gmt":"2020-04-27T16:31:36","slug":"coding-interview-question-two-sums","status":"publish","type":"post","link":"https:\/\/markedcode.com\/index.php\/2020\/04\/17\/coding-interview-question-two-sums\/","title":{"rendered":"Two Sums &#8211; Coding interview question"},"content":{"rendered":"\r\n<div class=\"wp-block-image\">\r\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-579\" src=\"https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/code.jpg\" alt=\"Cover photo - code\" width=\"275\" height=\"183\" \/><\/figure>\r\n<\/div>\r\n\r\n\r\n\r\n<h1><strong>Two Sums<\/strong><\/h1>\r\n<p>I will be starting a series of blog posts on Wednesdays where I break down and review common computer science interview questions so we can all work on our data structure and algorithm knowledge.\u00a0Today we will start the series with what might be the most common coding interview question: Two Sums. The question goes like this.<\/p>\r\n\r\n\r\n\r\n\r\n\r\n<p>\u201cGiven an array of integers, return indices of the two numbers such that they add up to a specific target.<br \/>You may assume that each input would have exactly one solution, and you may not use the same element twice.\u201d<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-580\" src=\"https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/Example.png\" alt=\"Two Sums example cases\" width=\"344\" height=\"146\" srcset=\"https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/Example.png 344w, https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/Example-300x127.png 300w\" sizes=\"auto, (max-width: 344px) 100vw, 344px\" \/><\/figure>\r\n\r\n\r\n\r\n<p>We will be running these questions on LeetCode. If you want to try the coding interview question: Two Sums for yourself first, go to <a href=\"https:\/\/leetcode.com\/problems\/two-sum\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/leetcode.com\/problems\/two-sum\/<\/a>.<\/p>\r\n\r\n\r\n\r\n<p>We will be solving this problem is Java. It is important to think through these problems first, write some pseudo code on paper and try to come up with the most efficient solution.<br \/>*cough* don&#8217;t use two for loops *cough*<\/p>\r\n<h1><strong>Code<\/strong><\/h1>\r\n\r\n\r\n\r\n<p>Here is what the boiler plate method will look like for us:<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-gallery columns-1 is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\">\r\n<ul class=\"blocks-gallery-grid\">\r\n<li class=\"blocks-gallery-item\">\r\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-581\" src=\"https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/BoilerPlate.png\" alt=\"Two sums boiler plate code\" width=\"476\" height=\"140\" data-id=\"581\" data-full-url=\"https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/BoilerPlate.png\" data-link=\"https:\/\/markedcode.com\/?attachment_id=581\" srcset=\"https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/BoilerPlate.png 476w, https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/BoilerPlate-300x88.png 300w\" sizes=\"auto, (max-width: 476px) 100vw, 476px\" \/><\/figure>\r\n<\/li>\r\n<\/ul>\r\n<\/figure>\r\n\r\n\r\n\r\n<p>We will be using a HashMap for this solution. It is a very common data structure and seen frequently in interview questions. HashMaps have constant look up time, which we will be using to look for values as we iterate through our input array (nums).<\/p>\r\n\r\n\r\n\r\n<p><br \/>While we are here, lets go ahead and create our return array and return it.<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-gallery columns-1 is-cropped wp-block-gallery-2 is-layout-flex wp-block-gallery-is-layout-flex\">\r\n<ul class=\"blocks-gallery-grid\">\r\n<li class=\"blocks-gallery-item\">\r\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-582\" src=\"https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/Start.png\" alt=\"Initialize variables \" width=\"603\" height=\"181\" data-id=\"582\" data-full-url=\"https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/Start.png\" data-link=\"https:\/\/markedcode.com\/?attachment_id=582\" srcset=\"https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/Start.png 603w, https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/Start-300x90.png 300w\" sizes=\"auto, (max-width: 603px) 100vw, 603px\" \/><\/figure>\r\n<\/li>\r\n<\/ul>\r\n<\/figure>\r\n\r\n\r\n\r\n<p>So our basic algorithm will be this. We will iterate through our array and for each value we will check to see if we have already found a value that will sum the current value to the target value.<br \/>For the above example. We will look at 2 and try to find 9 (Target) \u2014 2 (current value) = 7 (have we seen a 7 yet?). If we have seen the value we can save off those index\u2019s. If have not we can save the value<br \/>of the current location into our HashMap (value,index) and move to the next value. Remember, we need to save the index for each value because the final array returned wants the indexed location of each value in the array, not the value\u2019s that sum to the target.<\/p>\r\n\r\n\r\n\r\n<p>Note: This solution is not technically the fastest but is easy to grasp conceptually.<\/p>\r\n\r\n\r\n\r\n<p>So lets make the algorithm real by looping through our array.<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-583\" src=\"https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/Loop.png\" alt=\"for loop\" width=\"625\" height=\"237\" srcset=\"https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/Loop.png 625w, https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/Loop-300x114.png 300w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/figure>\r\n\r\n\r\n\r\n<h1><strong>Hash Maps<\/strong><\/h1>\r\n<p>For each index we will look for the current values pair that sums to target. If we find it, we can get the index of the paired value from our map and the current index and add them to the res array we are returning.<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-584\" src=\"https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/FoundIT.png\" alt=\"find match\" width=\"640\" height=\"289\" srcset=\"https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/FoundIT.png 640w, https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/FoundIT-300x135.png 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\r\n\r\n\r\n\r\n<p>If we do not find the paired value to sum to our target, we can add the value and index of our current record to our map. This way we can remember that we have come across this value later.<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-585\" src=\"https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/AllCode.png\" alt=\"Two sums - add to map\" width=\"665\" height=\"370\" srcset=\"https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/AllCode.png 665w, https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/AllCode-300x167.png 300w\" sizes=\"auto, (max-width: 665px) 100vw, 665px\" \/><\/figure>\r\n\r\n\r\n\r\n<h1><strong>Tests<\/strong><\/h1>\r\n<p>We can submit our code and see the results. When submitting code on LeetCode a variety of test are run against your code to hit all edge cases. Success! We passed, and now have a valuable tool in our belt: HashMaps.<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-586\" src=\"https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/Results.png\" alt=\"Two Sums success\" width=\"543\" height=\"165\" srcset=\"https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/Results.png 543w, https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/Results-300x91.png 300w\" sizes=\"auto, (max-width: 543px) 100vw, 543px\" \/><\/figure>\r\n\r\n\r\n\r\n<p>Here is a video explaining the logic behind this problem:<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\r\n<div class=\"wp-block-embed__wrapper\">https:\/\/www.youtube.com\/watch?v=wpUZRppjab8&amp;feature=emb_logo<\/div>\r\n<\/figure>\r\n\r\n\r\n\r\n<p>For more tech blogs, subscribe to the code_marks news letter: <a href=\"http:\/\/eepurl.com\/gZCMQz\" target=\"_blank\" rel=\"noreferrer noopener\">http:\/\/eepurl.com\/gZCMQz<\/a><\/p>\r\n<p><a href=\"https:\/\/markedcode.com\/index.php\/2020\/04\/22\/coding-interview-question-reverse-string\/\">Check out the next interview\u00a0 question!<\/a><\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>Two Sums I will be starting a series of blog posts on Wednesdays where I break down and review common computer science interview questions so we can all work on our data structure and algorithm knowledge.\u00a0Today we will start the series with what might be the most common coding interview question: Two Sums. The question<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[8],"tags":[14],"class_list":{"0":"post-578","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-cpsc-interview-questions","7":"tag-cpsc-interview-question-leetcode-two-sums-java"},"_links":{"self":[{"href":"https:\/\/markedcode.com\/index.php\/wp-json\/wp\/v2\/posts\/578","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/markedcode.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/markedcode.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/markedcode.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/markedcode.com\/index.php\/wp-json\/wp\/v2\/comments?post=578"}],"version-history":[{"count":4,"href":"https:\/\/markedcode.com\/index.php\/wp-json\/wp\/v2\/posts\/578\/revisions"}],"predecessor-version":[{"id":655,"href":"https:\/\/markedcode.com\/index.php\/wp-json\/wp\/v2\/posts\/578\/revisions\/655"}],"wp:attachment":[{"href":"https:\/\/markedcode.com\/index.php\/wp-json\/wp\/v2\/media?parent=578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/markedcode.com\/index.php\/wp-json\/wp\/v2\/categories?post=578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/markedcode.com\/index.php\/wp-json\/wp\/v2\/tags?post=578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}