• C++ Programming for Financial Engineering
    Highly recommended by thousands of MFE students. Covers essential C++ topics with applications to financial engineering. Learn more Join!
    Python for Finance with Intro to Data Science
    Gain practical understanding of Python to read, understand, and write professional Python code for your first day on the job. Learn more Join!
    An Intuition-Based Options Primer for FE
    Ideal for entry level positions interviews and graduate studies, specializing in options trading arbitrage and options valuation models. Learn more Join!

UBS quant interview question

Joined
5/27/09
Messages
106
Points
28
Someone I know told me this was at the interview with UBS recently:

The number 1978 is such a number that if you add the first 2 sets of numbers, you'll will get the middle 2 sets of numbers. So in 1978, 19+78=97; so the question is write a formula that can find numbers that satisfy these conditions.

PS - I don't know the solution.
 
The most I can say is: if you label the digits as abcd, such that a =1, b=9, c=7 and d=8 for example, then:
10a + b = first set of numbers and
10 c + d = 2nd and
10b + c = middle, then since

1s+2nd = middle, then

10a + b + 10c + d = 10b + c, therefore

10a -9 (b-c) + d = 0.

However this can only be used to verify that the digits fulfil the requirements; I am not convinced that this is a formula though. All I can come up with.
 
(y-1)x + (x-y)(10-y) where x>y, x-y>=2, y>1

Took me around 30 minutes to derive it mostly by finding another number that fulfilled the properties mentioned and looking for patterns in the digits.
 
hildingur.mahanti;54049 said:
(y-1)x + (x-y)(10-y) where x>y, x-y>=2, y>1

Took me around 30 minutes to derive it mostly by finding another number that fulfilled the properties mentioned and looking for patterns in the digits.


ok; but what do your x and y represent?
 
the question i'm assuming is asking for 4 digit numbers satisfying this property? the statements "first two sets of numbers" and "middle two sets of numbers" aren't precise otherwise.

so you're looking for numbers abcd.

10a+b+10c+d=10b+c, which can be rewritten 10a+d=9(b-c). Note that the LHS is just the two digit number ad, and it's a two digit multiple of 9 (because the RHS is 9(b-c)). so basically the 4 digit numbers in question are built by

1) taking a two digit multiple of 9, and using those two digits as the first and the last of the 4 digit number.

2) dividing the number in step 1) by 9 and picking two digits b and c (the middle ones) whose difference is the result.

e.g., take 54. then 5 and 4 will be the first and last digits. 54/9=6, so the middle digits should have a difference of 6. possibilities:

5604, 5714, 5824, 5934
 
the question i'm assuming is asking for 4 digit numbers satisfying this property? the statements "first two sets of numbers" and "middle two sets of numbers" aren't precise otherwise.

so you're looking for numbers abcd.

10a+b+10c+d=10b+c, which can be rewritten 10a+d=9(b-c). Note that the LHS is just the two digit number ad, and it's a two digit multiple of 9 (because the RHS is 9(b-c)). so basically the 4 digit numbers in question are built by

1) taking a two digit multiple of 9, and using those two digits as the first and the last of the 4 digit number.

2) dividing the number in step 1) by 9 and picking two digits b and c (the middle ones) whose difference is the result.

e.g., take 54. then 5 and 4 will be the first and last digits. 54/9=6, so the middle digits should have a difference of 6. possibilities:

5604, 5714, 5824, 5934


uhmmm......Your answer seems to be the same as mine, so why repeat my solution, or I guess I can also say, 'Great minds thinks alike'.. maybe you did not see my solution below. Good thinking process though.
 
i didn't actually see it. but now that i looked, your solution does use the same approach (and it's really the *only* approach to get a formula...) but doesn't completely describe how to get all solutions...

if you want a formula (and this can be easily deduced from my last post), it would be ([x][y][y-x-1][9-x]) (these are digits), where (1\leq x \leq 9), and (x+1 \leq y \leq 9). e.g., 1978 is gotten by picking x=1, y=9.
 
i didn't actually see it. but now that i looked, your solution does use the same approach (and it's really the *only* approach to get a formula...) but doesn't completely describe how to get all solutions...

if you want a formula (and this can be easily deduced from my last post), it would be ([x][y][y-x-1][9-x]) (these are digits), where (1\leq x \leq 9), and (x+1 \leq y \leq x+10). e.g., 1978 is gotten by picking x=1, y=9.

Yes, I guess this is a better/more sophisticated way of putting. I have not looked over your math, but from a quick glance, I can see you are just using some basic algebra to make it look better. I would actually prefer to stick with my approach and your earlier approach because sometimes there is some greater efficacy in simplicity. Then allow a computer to do the rest.

Actually with a quick glance there seems to be an error in your limits; you say y's upper limit is x + 10, but this means that why can go to at least 11 (since the minimum x is 1). This is precisely why simplicity is king.
 
you're right, the upper limit should be 9; I've changed it and now it's entirely correct. I don't see how my formula is not simple though. in fact, it's simpler than the initial description because all you do is plug in values for x and y and get a number as a result.

remember, a formula should be something that *directly* generates solutions. or else anyone could come up with a "formula" just by saying, hey just check every value starting with 1
 
Yes, all these are pretty much equivalent. It may be easier to think about generating the final number from the choices of the middle digits (b) and (c). The choice is subject to the constraint: (b > c+1), and of course both must be valid digits.

After that, you simply take 9 times the difference to generate the first and last digits.
 
you're right, the upper limit should be 9; I've changed it and now it's entirely correct. I don't see how my formula is not simple though. in fact, it's simpler than the initial description because all you do is plug in values for x and y and get a number as a result.

remember, a formula should be something that *directly* generates solutions. or else anyone could come up with a "formula" just by saying, hey just check every value starting with 1

I do agree with you fully. The limit was the only problem, but now your answer is good and probably more of a formula than mine. Good.

---------- Post added at 11:02 AM ---------- Previous post was at 11:02 AM ----------

Yes, all these are pretty much equivalent. It may be easier to think about generating the final number from the choices of the middle digits (b) and (c). The choice is subject to the constraint: (b > c+1), and of course both must be valid digits.

After that, you simply take 9 times the difference to generate the first and last digits.

Nice strategy, I actually prefer this even more than mine.
 
Yes, I guess this is a better/more sophisticated way of putting. I have not looked over your math, but from a quick glance, I can see you are just using some basic algebra to make it look better. I would actually prefer to stick with my approach and your earlier approach because sometimes there is some greater efficacy in simplicity. Then allow a computer to do the rest.

Actually with a quick glance there seems to be an error in your limits; you say y's upper limit is x + 10, but this means that why can go to at least 11 (since the minimum x is 1). This is precisely why simplicity is king.

Here's my approach to check numbers, using modular arithmetic.

(\frac{[x-x\, mod\,10^{2}]}{10^{2}}+x\, mod\,10^{2}=\frac{x\, mod\,10^{3}-x\, mod\,10}{10})

Basically, if x is the number, and it is written as abcd, the first term is ab, the second term is cd, and the right side is bc.

If we want to generate such a number, in the realm of 4-digit numbers, simply realize that the the middle digits can go from 0 to 99. Therefore, if the number x is of the form abcd, we can generate numbers using the following algorithm:

1. Pick bc. We have one constraint: c must be less than or equal to b. If c is larger than b, ab+cd>bc always, since the second number we're adding is clearly larger than bc.
2. Now we find the second constraint, that is:
(ab+cd=\left[a*10+\frac{(bc-bc\, mod\,10)}{10}\right]+\left[(bc\, mod\,10)*10+d\right]=bc)

Or more simply:

(100a+10d=9*(bc-11*bc\, mod10))

Since we are generating numbers, this is our main goal. We know exactly how our unknowns relate to the middle number bc.

Now it is simple:
3. Pick d as follows:
(d=\frac{9}{10}*(bc-11*bc\, mod10)\, mod10)
4. Pick a as follows:
(a=[9*(bc-11*bc\, mod10)-10*d]/100)

If you generate b and c separately, simply let (bc-11*bcmod10)=(b-c)*10. This simplifies everything a little bit, we get:
3. Pick d as follows:
(d=[9*(b-c)]\, mod10)
4. Pick a as follows:
(a=[9*(b-c)-d]/10)

By the way, it's impressive that once the middle two digits are chosen, the rest of the number is defined. I initially thought that there were going to be several cases, but there aren't. This is obviously equivalent to Bob's, but I only read his post after finding my solution.

There you have it. :)
 
intuitive response

ABCD=D*10^0+C*10^1+B*10^2+A*10^3

working out the conditions all the solutions must satisfy the next formula

A*10+D=9*(B-C) ,

for example 1978 is one of them.
I suggest to start from right B-C=2,3,4,...,9 and to find A and D,
form here a simple counting argument give us all the possible solutions.

ciao,
ja
 
Here's my approach to check numbers, using modular arithmetic.

(\frac{[x-x\, mod\,10^{2}]}{10^{2}}+x\, mod\,10^{2}=\frac{x\, mod\,10^{3}-x\, mod\,10}{10})

Basically, if x is the number, and it is written as abcd, the first term is ab, the second term is cd, and the right side is bc.

If we want to generate such a number, in the realm of 4-digit numbers, simply realize that the the middle digits can go from 0 to 99. Therefore, if the number x is of the form abcd, we can generate numbers using the following algorithm:

1. Pick bc. We have one constraint: c must be less than or equal to b. If c is larger than b, ab+cd>bc always, since the second number we're adding is clearly larger than bc.
2. Now we find the second constraint, that is:
(ab+cd=\left[a*10+\frac{(bc-bc\, mod\,10)}{10}\right]+\left[(bc\, mod\,10)*10+d\right]=bc)

Or more simply:

(100a+10d=9*(bc-11*bc\, mod10))

Since we are generating numbers, this is our main goal. We know exactly how our unknowns relate to the middle number bc.

Now it is simple:
3. Pick d as follows:
(d=\frac{9}{10}*(bc-11*bc\, mod10)\, mod10)
4. Pick a as follows:
(a=[9*(bc-11*bc\, mod10)-10*d]/100)

If you generate b and c separately, simply let (bc-11*bcmod10)=(b-c)*10. This simplifies everything a little bit, we get:
3. Pick d as follows:
(d=[9*(b-c)]\, mod10)
4. Pick a as follows:
(a=[9*(b-c)-d]/10)

By the way, it's impressive that once the middle two digits are chosen, the rest of the number is defined. I initially thought that there were going to be several cases, but there aren't. This is obviously equivalent to Bob's, but I only read his post after finding my solution.

There you have it. :)

The "spirit" of all the different approaches to the solution is the same, and that one with modular arithmetic is, I think, the most "elegant". :)
However, it also seems to me just like killing a mosquito with a F35: it works, but there are lots of easier and faster methods to do it!

That gives me the input to ask a question. While solving a quantitative problem (with particular reference to Finance), what do you think are the main parameters to look at in order to find the best solution in terms of speed and usefulness?

Thanks.
 
The "spirit" of all the different approaches to the solution is the same, and that one with modular arithmetic is, I think, the most "elegant". :)
However, it also seems to me just like killing a mosquito with a F35: it works, but there are lots of easier and faster methods to do it!

That gives me the input to ask a question. While solving a quantitative problem (with particular reference to Finance), what do you think are the main parameters to look at in order to find the best solution in terms of speed and usefulness?

Thanks.

if you're looking for a cure-all in problem-solving, there is of course no such thing. you mostly have to build a feel for how things work through exposure and practice. but there are certain heuristic approaches that are often useful:

1) simplify your problem
2) look at extremes
3) look at small cases

3 sort of falls under 2 which sort of falls under 1. 3 is particularly useful for problems involving statements about the natural numbers, and often leads to an inductive/recursive solution.
 
Given the number can be written in the form abcd, we have up to two degrees of freedom:

1) Choose 0 <= a < 9
2) Set d = 9*ceil(10*a/9)-10*a
3) Choose (10*a+d)/9 <= b < 10
4) Set c = b - (10*a+d)/9
 
intuitive response

ABCD=D*10^0+C*10^1+B*10^2+A*10^3

working out the conditions all the solutions must satisfy the next formula

A*10+D=9*(B-C) ,

for example 1978 is one of them.
I suggest to start from right B-C=2,3,4,...,9 and to find A and D,
form here a simple counting argument give us all the possible solutions.

ciao,
ja

This is what I came with too, very simple and complete response.
36 possible numbers.
 
10*a+d=9*(b-c)
a>=0,d>=0, so 0<=b-c<=9, so there are 10 numbers for 10*a+d for b-c={0,1,..,9}
for b-c=0 there are 9 combinations
for b-c=1 there are 8 combinations

for b-c=9 there is 1 combination
so total 45 numbers
 
I didn't count b-c=0 and b-c=1 because in these cases it's not a 4 digit number.
If you count these cases, then you have 54 numbers. (edit 55 if you count 0000)

And there are a few errors in your counting...
 
Back
Top