{"id":22,"date":"2022-02-11T07:06:49","date_gmt":"2022-02-11T07:06:49","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/linhsin\/?p=22"},"modified":"2022-02-11T07:06:49","modified_gmt":"2022-02-11T07:06:49","slug":"two-questions-i-was-asked-in-an-interview","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/linhsin\/2022\/02\/11\/two-questions-i-was-asked-in-an-interview\/","title":{"rendered":"Two Questions I was asked in an interview"},"content":{"rendered":"<p>Taking a break from our capstone project, I thought it would be fun to share two questions I was asked when I interviewed for my previous Software Engineer position.<\/p>\n<h1>Technical or not technical?<\/h1>\n<p>I don&#8217;t remember what phrase my manager used to describe those questions during my interview. They&#8217;re not questions regarding your knowledge of any programming language, and they don&#8217;t really require you to code. When it&#8217;s my turn to interview new entry-level software engineers, I still asked these two questions (it somehow became a tradition in the dev team recruitment), and I called them &#8220;problem-solving&#8221; questions. So what are the questions?<\/p>\n<h2>Two Integers<\/h2>\n<p><strong>You have two integer variables, a and b, both were assigned a value, how do you swap their values without using any extra space, i.e. without declaring a third variable?<\/strong><\/p>\n<h2>Random Integer Generator<\/h2>\n<p><strong>You have a function that generates a perfectly random integer between 1 to 5 (inclusively). How can you use this function to generate a perfectly random integer between 1 to 25 (also inclusively)?<\/strong><\/p>\n<p>Why I love these two questions? They are both a little bit tricky since they block out the most obvious solutions. No, you cannot create an integer c to temporarily store the value of either a or b. No, there isn&#8217;t a function that returns the answer for you. Both questions require you to take a detour and find a different solution. If you come up with a solution right away, good for you! Then you might be amazed how many candidates got stuck with them. A candidate once asked why I was asking these questions. What&#8217;s the point not using the most obvious way? Well, I love these questions since it gives me a hint about how a candidate solves a new problem. Can you utilize whatever you have to crack the problem? What approaches could you try to solve it? Can you think outside of the box? I believe how a candidate deals with these problems reveals how they would perform when they&#8217;re facing real-world problems.\u00a0<\/p>\n<h1>Answers<\/h1>\n<p>If a candidate got stuck on the first question, The first hint I always gave was &#8220;think of it as a math question&#8221;. Still stuck? &#8220;Why don&#8217;t you start by adding b&#8217;s value to a? What you be your next step to solve it?&#8221; You probably have figured it out now (or at the beginning) &#8211; we can&#8217;t use extra space, but we can manipulate the values. Here&#8217;s one way to solve it:<\/p>\n<p>a = a + b<\/p>\n<p>b = a &#8211; b (we get b equals a&#8217;s original value)<\/p>\n<p>a = a &#8211; b (we get a equals b&#8217;s original value)<\/p>\n<p>The second question is a little trickier. Many first guess I got was random5() * 5. But can you get 17 out of it? No. So it&#8217;s not perfectly random. What about random5() + random5() + random5() + random5() + random5()? You won&#8217;t be able to get 1. We can think of 1 to 25 as 5 groups of 5 integers, group 1 is 1 to 5, group 2 is 6 to 10, and so on. How about we get a random 1 to 5 to decide the group first, and we get another to decide the integers within the group? This sounds perfectly random for me! The answer can be written as:<\/p>\n<p>(random5() &#8211; 1) * 5 + random5()<\/p>\n<p>\u00a0<\/p>\n<p>These questions are not hard at all once you know the answers, right?<\/p>\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Taking a break from our capstone project, I thought it would be fun to share two questions I was asked when I interviewed for my previous Software Engineer position. Technical or not technical? I don&#8217;t remember what phrase my manager used to describe those questions during my interview. They&#8217;re not questions regarding your knowledge of &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blogs.oregonstate.edu\/linhsin\/2022\/02\/11\/two-questions-i-was-asked-in-an-interview\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Two Questions I was asked in an interview&#8221;<\/span><\/a><\/p>\n","protected":false},"author":12029,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-22","post","type-post","status-publish","format-standard","hentry","category-general"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/linhsin\/wp-json\/wp\/v2\/posts\/22","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/linhsin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/linhsin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/linhsin\/wp-json\/wp\/v2\/users\/12029"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/linhsin\/wp-json\/wp\/v2\/comments?post=22"}],"version-history":[{"count":2,"href":"https:\/\/blogs.oregonstate.edu\/linhsin\/wp-json\/wp\/v2\/posts\/22\/revisions"}],"predecessor-version":[{"id":26,"href":"https:\/\/blogs.oregonstate.edu\/linhsin\/wp-json\/wp\/v2\/posts\/22\/revisions\/26"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/linhsin\/wp-json\/wp\/v2\/media?parent=22"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/linhsin\/wp-json\/wp\/v2\/categories?post=22"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/linhsin\/wp-json\/wp\/v2\/tags?post=22"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}