Portfolio Management puzzles, solved step by step
- Puzzles
- 100
- Traced to a firm
- 31
- Topics
- 13
- Hard
- 30
016A stock's prices over six days are 7, 1, 5, 3, 6 and 4. What is the most you can make with one buy followed by one later sell? And what is the most with any number of buy and sell trades, if you can hold at most one share and cannot short?Man GroupLondon · 2019
Try it first
With unlimited trades, what is the maximum profit?
Show the worked solution
5 with one trade and 7 with any number. With one trade, buy at the lowest price that comes before a higher one: buy at 1 on day 2 and sell at 6 on day 5, for 5. With unlimited trades, collect every day-to-day rise and sit out every fall: 1 to 5 earns 4, 3 to 6 earns 3, a total of 7. The general answer is the sum of the positive daily changes.
How do you find the best single trade without checking every pair?
Walk through the prices once, like a shopper who notes the cheapest price seen so far and asks each day how much they would make selling today. The best single trade is the largest gap between today's price and the lowest price seen before today, found in one pass. At day 2 the low is 1. Day 3 offers 5 minus 1, which is 4; day 5 offers 6 minus 1, which is 5; no later day beats it. The trap is subtracting the lowest price from the highest overall: the high of 7 comes before the low of 1, so it cannot be sold after buying.
On prices of 7, 1, 5, 3, 6 and 4, one trade catches the widest later gap, buying at 1 and selling at 6 for 5, while unlimited trades catch each rise, 4 and then 3, for 7. Why is the unlimited answer just the sum of the rises?
Any profitable trade from a low to a later high can be split into daily steps, and it gains only on the up days inside it while paying for every down day it sits through. With no limit on trades and no shorting, the best strategy holds the stock on every day it rises and nothing on every day it falls, so profit equals the sum of positive daily changes. Here that is 4 plus 3, which is 7. One pass through the prices gives the answer, which is the point of asking a coding-flavoured candidate.
The relationshipp_t the price on day t \max(p_{t+1}-p_t, 0) a day's rise, or zero on a falling day What it says in wordsOne trade is the widest later gap; unlimited trades collect every daily rise.Say the limitation as a portfolio manager would: this is perfect hindsight with no costs. Add a transaction cost per trade and the two answers move toward each other, because catching a small rise is no longer worth paying for.
Where candidates lose it
The common mistake is answering 6, the highest price minus the lowest, without checking that the high comes after the low. The interviewer is testing whether you respect the order of time.
For the second part, candidates sometimes add the falls as well, answering 9 or more, as if they could short. Reread the constraint: no shorting means falls are only avoided, never earned.
What the interviewer asks next
- What if you are allowed at most two trades?
- Each trade now costs 1. What is the best total?
- Write the one-pass algorithm for the single trade and say its running time.
Asked at Man Group, Alternative Investments, London, 2019 (Wall Street Oasis):
Given a series of prices, find the one buy/sell trade pair which gives the maximum profit
088An n by n by n cube, like a Rubik's cube, is built from small cubes. As a formula in n, how many of the small cubes show at least one face on the outside?T. Rowe PriceBaltimore · 2020
Try it first
For n = 3, a standard Rubik's cube, how many small cubes show a face?
Show the worked solution
n cubed minus (n minus 2) cubed, which expands to 6n squared minus 12n plus 8. Everything not on the surface forms a hidden cube with one layer peeled from each side, so its side is n minus 2. For n = 3 that is 27 minus 1, or 26; for n = 10 it is 488. If the interviewer means the coloured squares instead, the answer is 6n squared.
Why count what you cannot see?
To count the tiles round the edge of a courtyard, it is easier to measure the whole yard and subtract the inner lawn than to walk the border without counting corners twice. The cubes not on the surface form one clean block of side n minus 2, so subtracting it from n cubed avoids every double-counting trap at the edges and corners.
In a 3 by 3 by 3 cube only the centre cube is hidden, so 26 of the 27 show a face; the same subtraction of a hidden core of side n minus 2 gives 8, 26, 56, 98 and 488 for n equal to 2, 3, 4, 5 and 10. The relationshipn^3 all the small cubes (n-2)^3 the hidden core after peeling one layer from every side What it says in wordsSurface cubes are all cubes less the core you cannot see.How do you check the formula a second way?
Count by type. There are always 8 corner cubes, 12 edges each carrying n minus 2 cubes between the corners, and 6 faces each with an (n minus 2) by (n minus 2) centre. That is 8 plus 12(n minus 2) plus 6(n minus 2) squared, which expands to 6n squared minus 12n plus 8. Two routes agreeing is the check the interviewer wants to hear.
Two edge cases show care. The formula needs n of at least 2; a single cube, n = 1, is all surface, and the formula would wrongly give 2. And the question is sometimes asked as surface area: the number of coloured squares is 6n squared, 54 for a standard cube. Ask which is meant before answering.
Where candidates lose it
The fast wrong answer is 6n squared, which counts the stickers, not the cubes: every edge cube is counted twice and every corner three times. On a standard cube that gives 54 against the true 26.
The second loss is giving the formula with no check. Say the corner, edge and face split once; it takes ten seconds and proves the algebra.
What the interviewer asks next
- How many small cubes show exactly two faces, as a formula in n?
- For what n are more than half the cubes hidden?
- Now the cube is n by n by m. Generalise the count.
Asked at T. Rowe Price, Equities, Baltimore, 2020 (Wall Street Oasis):
Give an equation that yields the surface area of an n by n by n Rubic's cube based on number of blocks per side.
