30

I was asked this logic puzzle in an interview.

Tell me a correct way by which adding 22 to 4 will give 2.

I asked interviewer what is the base of those numbers. He told that those are decimal numbers. I thought about it a lot, but could not find the answer. Any help would be appreciated. Thanks.

P.S. He also asked me to prove Oct 31 = Dec 25, which I did successfully. However, this question made me ask about base of those numbers. However, it is not related to question.

boboquack
  • 21,982
  • 1
  • 66
  • 138
A J
  • 3,974
  • 5
  • 29
  • 58
  • Was it oral or written ? – Fabich May 26 '16 at 07:59
  • Are we supposed to solve the P.S. first to understand how to solve the puzzle, or do we have to solve the puzzle first and use the P.S. as a confirmation ? – Mayo May 26 '16 at 08:10
  • 27
    Crazy questions. Don't take the job unless job requires you to generate/solve puzzles. – Mohit Jain May 26 '16 at 08:32
  • 30
    the correct answer to this question is "What is the real life scenario where you needed this?" – Marius May 26 '16 at 08:40
  • 8
    P.S. Why did the programmer confuse halloween with christmas? – Digital Trauma May 26 '16 at 21:06
  • 2
    I can't resist asking: what were you being interviewed for? – jpmc26 May 27 '16 at 00:16
  • @atkins, oh god true! head in the clouds ... apologies – Lamar Latrell May 27 '16 at 19:16
  • @a-j It's a bit worrying that you came away without knowing the answer. A good interviewer would have guided you towards the answer, not just left it open and moved on. – Isvara Feb 10 '17 at 05:13
  • @Derecho That's what I also expected, but he didn't do and moved to next question. I also asked for the answer in the end, but he didn't tell. Possibly he was going to ask the same question to other candidates. – A J Feb 10 '17 at 05:16

12 Answers12

63

If you consider the numbers as

24 hour time

Then

$22 = 10\text{pm}\\10\text{pm} + 4 \text{ hours} = 2 \text{am}$

KoA
  • 2,440
  • 1
  • 16
  • 28
  • 6
    I think this is almost certainly the right answer (and just a bit better than the suggestion of months) but I remark that whenever a,b,c are integers with c<a+b you can answer "how do you add a to b and get c?" with "by doing the addition modulo a+b-c"... – Gareth McCaughan May 26 '16 at 10:57
  • 6
    While this is most likely the right answer, the interviewer mislead by answering that "the numbers are decimal", which would mean they wrap around at 9+1. Which is very obviously not the case here. If I wrote hexadecimals as 0-15 that would still not make them decimal. – antipattern May 26 '16 at 12:04
  • 20
    @antipattern: Just because the question is using modular arithmetic doesn't mean it is not base 10. The hour resets after 23 but you can express that in decimal or whatever base you like. – Chris Burt-Brown May 26 '16 at 12:12
  • I have to agree with @antipattern. Hours are not decimal. Hours are either duodecimal (12) or tetravigesimal (24). In this nonsense is using both to make an addition (!) but clearly not using decimal (10). – Yiorgos Moschovitis May 26 '16 at 22:52
  • 1
    Huh? If these numbers are "not decimal", how many hours are there in a 24-hour day? – Jeffrey Kemp May 27 '16 at 02:19
  • two dozen hours in a day... – Yiorgos Moschovitis May 27 '16 at 08:50
  • And I have to agree with Chris Burt Brown. The numbers 22, 4 and 2 are decimal (base-10) numbers in this question. Period.If they were tetravigesimal (base-24) then 22<sub>tv</sub> (50<sub>d</sub>) + 4<sub>tv</sub> (4<sub>d</sub>) = 26<sub>tv</sub> (54<sub>d</sub>) which equals 2 in neither of bases.

    @antipattern if you wrote hexadecimal F as 15 and said it was a hexadecimal number it would make it 15h = 21d. If you said it was dec. it would be OK.

    – mstrz May 27 '16 at 11:17
  • @JeffreyKemp the fact that we express the 24 hours in a day in decimal format doesn't change the fact that a decimal unit is 1/10 and an hour is 1/24 (and a binary unit is 1/2, octal unit is 1/8, hex unit is 1/16...). – Jason May 27 '16 at 17:09
  • @mstrz the numbers 22, 4 and 2 are expressed in decimal format but they are not being used in a decimal system. It would be more appropriate to call them the numbers m, 4 and 2 in a number system from 0-n in the same way hex is a number system from 0-f - in this case, m + 4 = 12 - so still not entirely straight forward. – Jason May 27 '16 at 17:14
  • "22" is also not a time. The time would be 22:00. Same for "4" which would be represented as 04:00. So while I expect this is the correct answer, it's a terrible question. – Laconic Droid May 27 '16 at 19:56
  • Also, 22:00 plus 4 hours does not produce 02:00. It yields 02:00 plus one day. The same objection applies to the "months" answer. See "words that end in GRY": https://xkcd.com/169/ – Jamie Hanrahan May 28 '16 at 10:37
  • 1
    There is nothing base-24 or base-12 about time. Otherwise right now it would be A:AAam (base-12) or A:M am (base-24), but we don't say that, we say it is 10:22 (base-10). Hours, minutes, seconds are all expressed in decimal. However they are calculated modulo (24 for hours, 60 for minutes and seconds). Modulo != base. So to do 22+4 in terms of hours, the calculation would be: $(22+4)\pmod{24}=2$ - so it is still addition. – Tom Carpenter May 28 '16 at 14:23
  • @LaconicDroid I disagree with your reasoning. If 22 is not a time, why is 22:00 a time? You're just specifying it more precisely. Why aren't you required to specify it as 22:00:00? And obviously the same argument would follow until we reach the smallest amount of measurable time, which would lead into a discussion about the concept of time. So I argue that 22 IS a valid time, just like, for example, January 3rd is. Now, if I continue this: If it's Jan 3rd and you add 366 days (not a leap year), what date is it? You typically wouldn't add "... and a year". 22 + 4 hours = 2 o'clock seems fine. – Jakob Pamp Bengtsson Feb 10 '17 at 09:35
  • @JakobPampBengtsson You are free to disagree with the convention. But 22 is still not a time in the same way 22.344566 is not a time. If you have examples to cite where "22" is used as a time I'd be very surprised. – Laconic Droid Feb 10 '17 at 12:27
  • @LaconicDroid I, family member and friends here in Sweden use it. I'd say it's fairly prevalent. We interchangeably use "Klockan 10" and "Klockan 22" ([o'clock] effectively), though we can use shorthand in conversation; "Vilken tid?" "Runt 22" (When? Around 22), and even if it is uncommon to not use o'clock or something similar for further specification, I don't see a real problem with omitting it for puzzling purposes. – Jakob Pamp Bengtsson Feb 10 '17 at 12:29
35

I guess the interviewer is talking about

months : if you consider 4 as April (4th month in a year) and you add 22 other months (so you go 22 months after April), you get 2 because it's February (2nd month)!

Mayo
  • 777
  • 4
  • 11
  • @VincentAdvocaat Thank you ! The P.S. using months made me think about it, even though I don't understand how Oct 31 = Dec 25 – Mayo May 26 '16 at 08:25
  • 2
    I can explain it how Oct 31 = Dec 25. though it has nothing to do with my question. – A J May 26 '16 at 08:25
  • @AJ If you could, please, because it looks like it doesn't work with the same logic, even if you consider days of a month instead of months – Mayo May 26 '16 at 08:27
  • consider Oct and Dec number bases, you will get it. – A J May 26 '16 at 08:28
  • Oct = 10, Dec = 12, so you have 10/31 and 12/25 ; but 10+31 =/= 12+25, 31-10 =/= 25-12, 31 in base 10 =/= 25 in base 12 (=29) ; if you consider days of the month, 31+10=10 and 25+12=6... Didn't find the answer for the moment – Mayo May 26 '16 at 08:43
  • 8
    Oct(al) 31 equals Dec(imal) 25 – Micha Sprengers May 26 '16 at 08:46
  • @MichaSprengers Oh, yes it's that, well done – Mayo May 26 '16 at 08:47
  • 5
    The interviewer is therefore slightly deceitful when he/she says that the numbers are decimal, as 4 is effectively in base 12. – paolo May 26 '16 at 08:57
  • 15
    @paolo No, it is in base 10. From a mathematical standpoint, this is using modulo, not something to do with bases at all. If the question was: What is 7 added to 4, the answer would be 11, not B. Similarly, 22 added to 4 is 26 if both 22 and 4 were read in base 12, or 22 if they were read in base 10. That's not the point of the question though. The question asks: (4d + 22d) mod 12d = ? – Sumurai8 May 26 '16 at 09:35
  • @Menace Now I am thinking I should have asked oct 31 = dec 25 as another question. – A J May 26 '16 at 11:07
  • this is actually the same answer as the one that says $22 + 4 \equiv 2 \pmod{24}$, except using $\pmod{12}$, which evaluates the same – sig_seg_v May 26 '16 at 11:52
  • I hereby retract my statement as I just realized, 22+4 in base24 would be 12 not 2; It is not a question about bases, but the arithmentics behind it. – antipattern May 26 '16 at 12:18
24

Another more Programmatic solution

If 4 is an unsigned 3 bit integer adding 22 to it with overflows results in 2
4 = 100
4 + 1 = 101
4 + 2 = 110
4 + 3 = 111
4 + 4 = 000
..
4 + 22 = 010 = 2

Oleg
  • 438
  • 3
  • 6
  • 3
    Welcome to Puzzling Stack Exchange. This is, in essence, equivalent to a couple of the previous answers, but it provides a new angle, and is well explained. Good start! – Peregrine Rook May 26 '16 at 17:42
  • @PeregrineRook Thanks! I don't think this is equivalent to the other answers which add another operation(modulo). In this case you really do just add 22 to 4 and get 2 because of precision loss. – Oleg May 26 '16 at 18:12
  • 1
    But that's just a fancy way of saying that you're doing math modulo 8. – Peregrine Rook May 26 '16 at 18:22
  • 1
    @PeregrineRook There are not any answers using modulo 8 though. And, if it was a programming interview, this is the best answer imo. – jlars62 May 26 '16 at 20:48
  • 1
    I said that this answer is essentially equivalent (to the two answers that use $\mod 24$ and the two that use$\bmod 12$), not identical.  In general, this works for $\bmod b$ where $b,|,24$ ($b$ is a divisor of $24$), and so (since $8$ is a divisor of $24$) this answer is in the shadow of the $\bmod 24$ answers.  As to what the interviewer wanted, it’s impossible to say.  But the “P.S.” suggests that he was more interested in an out-of-the-box mathematical answer than a mundane computer-related solution. – Peregrine Rook May 27 '16 at 01:09
  • @PeregrineRook If I were asked in a programming interview a question that requires me to somehow understand the interviewer is talking about hours I would think he is an idiot for asking me stupid questions that have nothing to do with my profession and would rather work elsewhere. If the question was about integer overflow and precision loss(with more sensible numbers e.g. 120 + 10 = -126) I would think that it's a decent question which checks understanding of basic things that every programmer should know. – Oleg May 29 '16 at 00:31
  • Why 3 bits and not 2 bits? I think you chose 3 bits because you need 3 bits for 4, but you need 5 bits for 22. If you don't care about throwing away bits, then 2 bits gives the same answer, i.e. 4 (b00) + 22 (b10) = 2 (b10). – Mark Lakata May 31 '16 at 23:52
  • @MarkLakata It's perfectly fine to store the result of 22 + 4 in a 2 bit integer. That's another solution very similar to mine based on modulo 4. I chose 3 bits because that's what I thought about, I didn't think about 2 bits until I saw your comment. It doesn't matter how much bits you need for 22 you can narrow and widen in any way you want this is implementation detail and the result will be the same. I don't see the problem with throwing away bits, I'm storing a value in a 3(or 2) bit variable that doesn't fit in it and losing precision. If you want to call it "throwing away bits" you can. – Oleg Jun 01 '16 at 17:45
14

A more mathematical possibility is that

he is talking about modular arithmetic.

More specifically,

$22 + 4 \equiv 2 \pmod{24}$

mtvec
  • 241
  • 1
  • 4
  • This was my first thought, especially considering the oct 31/dec 25 question. (22 + 4 = 2 mod 8) – sig_seg_v May 26 '16 at 09:16
  • @sig_seg_v, can you explain how 10/31 = 12/25? OP never did. – user1717828 May 26 '16 at 10:55
  • 1
    @user1717828 Though this is not related to my question, but I would explain that. Consider Oct as Octal and Dec as Decimal. Now you have to convert octal number to decimal, or you can do vice versa. you will get the answer – A J May 26 '16 at 11:02
  • @user1717828 It's explained in a comment on another answer, but Oct and Dec refer to the base of the number (Octal and Decimal, respectively), rather than the months October and December. 31 in Octal (base 8) is 25 in Decimal (base 10). – Anthony Grist May 26 '16 at 11:03
  • Strictly speaking though, modulus is a division so if you are to add one more operation, why not substract 24 and be done withit? – Yiorgos Moschovitis May 26 '16 at 23:32
5

A possibility is that

$22+4 = 26$ and $26$ is a two digit number.

But I hope that there is a better answer there.

EDIT:

Another possibility is

how the interviewer might have said it. What you heard is "Tell me a correct way by which adding twenty two to four will give two.". But what he might have meant can be "Tell me a correct way by which adding twenty too to four will give two.". It tells us that there is another number which has been added to 20 and 4 that should give 2. So we have $20+4+x=2$. Therefore $x=-22$. So, when we add -22 in 20 and 4 then we get 2.

manshu
  • 6,304
  • 20
  • 56
  • It is a possibility, but he asked adding 22 to 4 will give 2. So I think he meant something like 2+4=6 – A J May 26 '16 at 07:57
  • 4
    The statement "Tell me a correct way by which adding twenty too to four will give two." makes no sense to me. – Warlord 099 May 26 '16 at 14:03
5

One way that I can think of is

place 22 before 4, put a decimal between the 2's and surround the whole number with the floor function.
$\lfloor 2.24 \rfloor$

CodeNewbie
  • 11,753
  • 2
  • 45
  • 87
  • 2
    $round(2.24)$ also works because $0.2<0.5$ – EKons May 26 '16 at 10:07
  • 1
    I don't think that add is the correct word to use. You should rather say "Place 22 before 4", and forget about doing things physically. – ahorn May 28 '16 at 16:59
2

KoA's answer seems good to me, but I see another interpretation of the wording:

4 is a time of day and 22 is a number of hours. This time, "adding 22 to 4" means adding 22 hours to 4pm, getting 2pm, or adding 22 hours to 4am, getting 2am. This is consistent with the 24-hour clock but doesn't require it.

Rosie F
  • 8,621
  • 2
  • 18
  • 50
  • This is essentially the same idea. – ahorn May 28 '16 at 17:05
  • 1
    @ahorn The use of arithmetic modulo 24 is the same, as is the interpretation in terms of days and hours. But mine differs in that I treat 4 as a time and 22 as an increment in hours (KoA did the reverse). So there is a difference in how the maths is interpreted in terms of times, as well as the fact (which I mentioned in my answer) that my interpretation doesn't rely on using the 24-hour clock. – Rosie F May 28 '16 at 18:33
2

Disclaimer

As a senior software engineer, here is my egoistic view of this. I do claim it is correct or what I say are facts. You have free will to interpret this as any way you want.

Preamble

A typical candidate has an inadequate set of skills for any available position. Therefore usually for first several months, he/she is put on tasks to develop themselves and might be producing company net loss. You have to make an estimation how long would it take for this person to acquire required set of skills.

This is a simple brain teaser, you could ask from an entry level candidate. It gives an estimation of what their cognitive dissonance is like. The basic idea is if you are good/great at processing problems your knowledge base inadequacy can be overlooked.

What would I look for when asking this question

  • did (s)he consider problems "solved" after finding a first working solution
  • did (s)he consider that "add" can have different meanings
  • did (s)he consider that "numbers" can have different contexts

I mentioned "good/great at processing problems", it is not the same as solving the problem. If the person failed you want to see how did (s)he respond to failure.

Introduction

+80% of software solution cost comes from maintenance after its development has ended. A typical enterprise code is read x10 times more than it is written. The best way to reduce the total cost is to write "clean code".

One aspect of writing "clean code" is to name things correctly. One of the simplest cases to demonstrate wording ambiguity is "add". Here are few different meanings of add (there are a lot more):

(you add 2 values)
1 + 1 = 2
(you add a value to each list value)            
{a, b, c} + 1 = {a + 1, b + 1, c + 1} 
(you add a value to start of the list)
Prepend[{a, b, c}, 1] = {1, a, b, c}
(you add a value to end of the list)
Append[{a, b, c}, 1] = {a, b, c, 1}
(you add a value to list at specified index position starting from the beginning)
Insert[{a, b, c}, 1, i] ex: {1, a, b, c} if i = 1
(you add a value to list at specified index position starting from the end)
Insert[{a, b, c}, 1, -i] ex: {a, b, c, 1} if i = 1
(you add a set of lists)
Join[{a, b, c}, {x, y}] = {a, b, c, x, y}
(you add a set of lists without duplicates)
Union[{a, b, c}, {c, a, d}] = {a, b, c, d}

In c# .net a list has a method Add for adding a single element and AddRange to add multiple elements. A single method would cause ambiguous meaning and would not be as clean.

When you implement a code on a 32bit architecture computer, there is a hardware support for bit 32-bit sequences and their basic arithmetics and logics. An integer of type int32 is implemented with 32 bits, it represents a discrete number having max value 2^31-1 and min value -2^31.

Solutions

Code examples in c# .net.

Contains spoilers.

Octo

I am not sure why his answer is voted down - it is correct.

1 baker's dozen = 13
2 baker's dozen = 26 (22 + 4)

Code :

const int BakersDozen = 13;

Value : Shows domain knowledge

manshu

Code :

public static int operator +(Manshu firstNumber, Manshu secondNumber)
    => (firstNumber + secondNumber).ToString().Count();

Value : Knows basic data types

CodeNewbie

Following code example hides (int)(22 / 10 + 4 / 10) to be 22 + 4

Code :

public static int operator +(CodeNewbie firstNumber, CodeNewbie secondNumber) 
    => firstNumber / 10 + secondNumber / 10;

Value : Knows data types / operators and maybe some functional programming

Oleg

Code :

public static int operator +(Oleg firstNumber, Oleg secondNumber) 
    => firstNumber > secondNumber ? firstNumber + secondNumber : 2;

Value : Knows about default fallbacks

Job

KoA and Menace are just sub cases of Job's general solution.

Code :

public static int operator +(Job firstNumber, Job secondNumber)
    => (firstNumber + secondNumber) % 24;

Value : Shows basic cryptography domain knowledge

Me

List that contains 22 and you add 4, now you have list containing 2 elements.

Code:

    var items = new List<int> { 22 };
    items.Add(4);

Value: Ability to read the question

Conclusion

A brain teasers during a job interviews can have valid uses. From my experience it is usually just a filler to engage the applicant.

Margus
  • 163
  • 3
  • @AJ I Provided my answer as well (List that contains 22 and you add 4, now you have list containing 2 elements.). Could enumerate several others, but this is easiest that was not mentioned. – Margus May 27 '16 at 13:42
  • 1
    How in hell did you manage to interpret my solution this way?! The correct implementation would be: public static Oleg operator +(Oleg firstNumber, int secondNumber) => new Oleg(firstNumber.intValue + secondNumber) and an Oleg(int intValue) constructor which does: this.intValue = intValue % 8 . I suggest that you learn about integer overflow, narrowing and widening. The world would be a better place if "senior software engineers" would know such basic things. – Oleg May 28 '16 at 16:00
  • 3
    How does using the modulo operator indicate "basic cryptography domain knowledge," or the fact that integer division truncates indicate "maybe some functional programming"? Nor does naming a constant BakersDozen indicate any "domain knowledge" of the confectionery industry. I really don't see what this answer contributes—it is a diatribe about software engineering practices followed by answers copied from other posters and some meaningless "value" judgments. At the top you probably meant that you "do not claim" that it is correct or factual, and I'm inclined to agree. – wchargin May 28 '16 at 21:34
  • @wchargin 1) "modular arithmetic". 2) idea you can apply "/10" function over arguments. 3) idea that 26 is 2 of something and knowing what that is. 4) I could have just presented a solution, but i chose to show my insight from other side of the table. Idea to take away is that, – Margus May 29 '16 at 06:49
  • 1
    KoA or Menace solutions might be highest rated, but Job solution is better. – Margus May 29 '16 at 06:52
  • @Margus The answerer has not demonstrated knowledge of "modular arithmetic" (e.g., what is the totient function, why is it important, and how is it related fundamentally to RSA?), nor map (performing two divisions is not seriously comparable to mapping an arbitrary function over an arbitrary list); yes, 26 is two of something, and one needs no domain knowledge to realize this fact or recognize that it has a name in common language (I am no baker—I have zero domain knowledge—and yet I know this term, for I speak English). – wchargin May 29 '16 at 06:52
  • @wchargin Yes, it is a simple difficulty task and does not require showing deeper understanding. Try to view this from entery level candidate perspective, that has not seen this problem before not yours. – Margus May 29 '16 at 07:47
  • Even 3rd graders could do this. – Margus May 29 '16 at 07:48
  • Unwilling to make assumptions, I refuse to answer the question on the grounds that it lacks context and there is not enough information. What do I get? – Mazura Jun 04 '16 at 12:27
0

adding twenty 2 to 4 will give 2.

20 * 2 + 4 = 44

So

44 is made from two 4 digits. "adding twenty 2 to 4 will give 2 (4's)".

Guillaume
  • 109
  • 2
0

22+4 = 2 x 13 (a bakers dozen)

Octo
  • 19
  • 1
  • 4
    explanation would not hurt. – A J May 26 '16 at 13:38
  • This answer, like the one preceding it, relies on a change of units -- the operands are in one unit while the result is in another. The unit for the calculation 22+4 is something like "bread rolls" and the unit for the result 2 is "a baker's dozen (ie 13)".

    In @Guillaume's the result must be interpreted with the units "number of 4's".

    One could just as accurately invent two units, fizbob and foobar, such that 22 + 4 fizbobs = 2 foobars. But it is not a very satisfying technique.

    – MattClarke May 27 '16 at 01:38
0

Military/24 hour time
22:00 (10pm) + 4 hours = 02:00 (2am)

Peregrine Rook
  • 4,880
  • 2
  • 26
  • 40
  • Welcome to Puzzling Stack Exchange. We have some rules here; I suggest that you take the [tour], maybe visit the [help], and read a few other people’s answers before you proceed. Among the rules are (1) hide your answer in “spoiler” markup, as I have demonstrated in your answer; and (2)  before posting your answer, read all the others. If yours is the same as one of more or the others, don’t post it, unless you’re really contributing something new.  Your answer is the same as a couple of the others. – Peregrine Rook May 27 '16 at 00:48
0

Considering 22 and 4 in hours format in a 24 hrs day cycle repesented in 24 hrs standard format

 Adding 22 hrs (10.00 pm) with 4 hrs (4.00 am) will give 22 + 4 =26 
 Pragmatically since we do not have 26 hrs we can split it as 24 + 2 which       
 in 12 hrs format is 12.00 (Midnight) and 2.00 am thus 26 hrs relatively is   
 2.00 am in 12 hrs as well as 24 hrs format

So the correct way might be adding the numbers in context with 24 hrs time format.

Newbee
  • 11