# Correct way to add 22 to 4 to get given value

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.

• Was it oral or written ? May 26 '16 at 7:59
• Crazy questions. Don't take the job unless job requires you to generate/solve puzzles. May 26 '16 at 8:32
• the correct answer to this question is "What is the real life scenario where you needed this?" May 26 '16 at 8:40
• P.S. Why did the programmer confuse halloween with christmas? May 26 '16 at 21:06
• I can't resist asking: what were you being interviewed for? May 27 '16 at 0:16

If you consider the numbers as

24 hour time

Then

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

• 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"... May 26 '16 at 10:57
• 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. May 26 '16 at 12:04
• @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. May 26 '16 at 12:12
• Huh? If these numbers are "not decimal", how many hours are there in a 24-hour day? May 27 '16 at 2:19
• 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. May 28 '16 at 14:23

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

• @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 8:25
• I can explain it how Oct 31 = Dec 25. though it has nothing to do with my question.
– A J
May 26 '16 at 8:25
• Oct(al) 31 equals Dec(imal) 25 May 26 '16 at 8:46
• The interviewer is therefore slightly deceitful when he/she says that the numbers are decimal, as 4 is effectively in base 12. May 26 '16 at 8:57
• @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 = ? May 26 '16 at 9:35

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

• 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! 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
• But that's just a fancy way of saying that you're doing math modulo 8. May 26 '16 at 18:22
• @PeregrineRook There are not any answers using modulo 8 though. And, if it was a programming interview, this is the best answer imo. May 26 '16 at 20:48
• 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. May 27 '16 at 1:09

A more mathematical possibility is that

he is talking about modular arithmetic.

More specifically,

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

• This was my first thought, especially considering the oct 31/dec 25 question. (22 + 4 = 2 mod 8) May 26 '16 at 9:16
• @sig_seg_v, can you explain how 10/31 = 12/25? OP never did. May 26 '16 at 10:55
• @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). 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? May 26 '16 at 23:32

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.

• 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 7:57
• The statement "Tell me a correct way by which adding twenty too to four will give two." makes no sense to me. May 26 '16 at 14:03

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$

• $round(2.24)$ also works because $0.2<0.5$ May 26 '16 at 10:07
• 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. May 28 '16 at 16:59

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.

• This is essentially the same idea. May 28 '16 at 17:05
• @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. May 28 '16 at 18:33

## 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 };


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.

• @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. May 27 '16 at 13:42
• 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
• 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. 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, May 29 '16 at 6:49
• KoA or Menace solutions might be highest rated, but Job solution is better. May 29 '16 at 6:52

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)".

– A J
May 26 '16 at 12:08

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

• 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. May 27 '16 at 1:38

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

• Welcome to Puzzling Stack Exchange. We have some rules here; I suggest that you take the tour, maybe visit the help center, 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. May 27 '16 at 0:48

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.