Two Questions I was asked in an interview

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’t remember what phrase my manager used to describe those questions during my interview. They’re not questions regarding your knowledge of any programming language, and they don’t really require you to code. When it’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 “problem-solving” questions. So what are the questions?

Two Integers

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?

Random Integer Generator

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)?

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’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’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’re facing real-world problems. 

Answers

If a candidate got stuck on the first question, The first hint I always gave was “think of it as a math question”. Still stuck? “Why don’t you start by adding b’s value to a? What you be your next step to solve it?” You probably have figured it out now (or at the beginning) – we can’t use extra space, but we can manipulate the values. Here’s one way to solve it:

a = a + b

b = a – b (we get b equals a’s original value)

a = a – b (we get a equals b’s original value)

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’s not perfectly random. What about random5() + random5() + random5() + random5() + random5()? You won’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:

(random5() – 1) * 5 + random5()

 

These questions are not hard at all once you know the answers, right?

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *