I'm working on a coding problem that I was given for simulating a stock market simulator. I don't really know anything about the stock market, but the instructions state that stock market domain knowledge isn't necessary to complete this coding problem. I'm still a bit confused however, and was hoping someone here could help interpret this for me.

The problem is as follows:

You're given a list of buy and sell orders represented as [buy or sell, quantity that is for sell or for buy, price]. e.g., [sell, 50, 4.50]. The order in which these orders are given is in chronological order. You're also told that a buyer has a maximum price in which they will pay for the item, and correspondingly, the seller has a minimum price in which they will sell their order. As long as there is a set of buy and sell orders that match this criteria, a transaction will occur. The cost of the transaction is in accordance with whichever order appeared first. Simulate this with code.

There's a lot of ambiguities, and unfortunately, I wasn't given more information, but I'm not sure if these are actually ambiguities, or if it's just my ignorance due to lack of experience in this area.

Some things that I think are ambiguous are:

(1) Is the given price, a price per unit quantity, or for the entirety of the quantity? e.g., is 4.50 for the entire 50 units, or is it $4.50 per unit (so 4.50*50 for the entire order)? Is there some kind of industry standard for this?

(2) We are told that the seller and buyers have their minimum and maximum bounds for selling and buying their desired item. Is this bound the price that is given? e.g., [sell, 50, 4.50] means that that specific seller will not go below 4.50? If so, that's kind of confusing to me? Why would they broadcast their lowest price instead of marking it up? On the other hand, if that's not their lowest price, then I assume that the given price is just their asking price, but then I guess we would need to be provided with their lowest price somehow?

(3) Can you partially fulfill an order? e.g., say the first order is [sell, 10, 5.50] and the second order is [buy, 8, 6.00]. Can the buyer simply get 8 units from the seller (i.e., fulfill his buy order entirely and then leave that sell order with 2 units?), or do they have to buy all 10 orders?

This isn't "supposed" to be a difficult problem, and I don't think it would be algorithmically. I just don't understand the question's parameters.

I tagged "orderbook" but I don't think this problem is the classical order book problem?


1 Answer 1


The price is given per unit, the quantity gives you the available size or depth.
The actors of the market will give you the lowest at which they are ready to sell and highest at which they are willing to buy as it translate their vision of the asset value, and they don't want to loose money. The system can match them if there is a counterparty willing to buy their sell order for a higher price than the one the specified, so no interest to pump your price. If you have troubles understanding I recommend you to check up Market Microstructure for Practitioners from Larry Harris Chapter 6, Order-driven markets.

For the partial filling of the order it should depend on the type of order as it can be specified that it should be executed as a whole or can be partially filled. Given that you don't have the information I would consider that you should fill as much orders as possible even partially.

  • $\begingroup$ Ah this all makes sense! I try to upvote, but it says I do not have enough reputation points to do that. I may check out that chapter. The stuff in the OP was one of several parts to the problem. I'm even more confused about the meaning of the later parts, which includes stuff about market orders, stop limits and such -- would these things be talked about in that chapter too? $\endgroup$ Aug 16, 2021 at 16:22
  • $\begingroup$ Also, it seems a max heap (for buyers) and a min heap for (sellers) is the best approach in terms of algorithmic implementation. This seems to maximize each new sell order's profit or minimize each new buy order's "loss" $\endgroup$ Aug 16, 2021 at 17:04
  • $\begingroup$ The different order types are not covered in your problematic as you have no information. Basically a market order is a liquidity taker, getting the current price of the market as you want your order to be executed now. A limit order is an order that will be executed only if the price reach a given level. Refer to the chapter 4 Order and Order Properties for a more detailed view $\endgroup$
    – Mayeul sgc
    Aug 17, 2021 at 0:32
  • $\begingroup$ Another question I have is, say we have two sell orders in the following order. Price 25 comes in first, price 10 comes in second. Then say we have 2 buy orders come in that are priced at 26 and price 5, respectively. Assume all quantities are 1. If we use the max and min heap idea (as I've seen in other sources), then that means the 26 will be matched with the 25, and the second buy order (price 5) won't be able to be matched with the remaining seller order. So in other words, it seems the two heap approach isn't trying to maximize the number of filled orders $\endgroup$ Aug 17, 2021 at 19:25
  • $\begingroup$ When I think about the real life implications of this, I think it makes sense. If I'm a buyer and I go look at what sell order to buy. I'm going to go for the lowest. I don't care who's behind me that's going to buy next. $\endgroup$ Aug 17, 2021 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.