Yesterday at work I came across an interesting problem that I wasn't able to figure how to structure properly. I'm relatively new to the field and was wondering if I could get some help around here. What i have here is a table like this:

          |1-2| 3-5 | 6+ | ($)
          |              |
10 - 100  | x9   x6   x4 |
          |              |
100 - 2000| x5   x3   x2 |

Where the columns shows the base price and the row shows the quantity. The idea here is that the bigger quantity you buy, the better the price. Not only that but we would also reduce the multiplier to items with bigger base price, because otherwise the price could become too high too quickly.

The outcome I want to achieve is when given two variables - base price and quantity to find where I'm standing in the table. This means for each table entry I need two ranges.

Let me give some examples:

  • Base price: 1.5$
  • Quantity: 200
  • Result: x5 (multiplier)

So the actual price will be 7.5$

  • Base price: 10$
  • Quantity: 15
  • Result: x4

So the actual price will be 40$

How would you remember the data and how would you write something flexible and scalable so one is able to add and remove table rows easily?

I would evaluate the answers based on how clean the solution is. And by clean I mean easy to use and easy to implement. Currently in our system this table is made from 20 if statements so anything would be better I think!

  • $\begingroup$ You seem to ask how to find corresponding ranges by a given number. If so, then reduce your question just to this, because the table is irrelevant to this issue. $\endgroup$ Jul 12 '18 at 10:00

It's hard to see anything beating the obvious implementation with arrays. You have an array whose columns are indexed by price bands and whose rows are indexed by quantity bands. Store that as a 2D array. Use a pair of 1D arrays to store the minimum value in each price and quantity band. To look up a value, just use linear search through the bands. Binary search would be faster if the array is huge but it's probably not necessary for small arrays (and your "20 if-statements" suggests the array is very small).

Load the data from a file so you can easily rebuild the data structure if you change your discounting schemes. I'm assuming this is a relatively rare event and, with a small table, it takes no time anyway.


Start with the simplest data structure, and use something fancier only if you can show that it will make a performance difference. A two-dimensional array would probably suffice, as would a hashmap (dict) that maps from (row number, column number) to the value in that cell. If you're really worried about adding and removing table rows, you could use a hashmap or treemap that maps from row identifier to a one-dimensional array with the items in that row.

  • $\begingroup$ It would definitely suit the situation, but let me clarify my question. Is more focused towards optimising the data structure so it's easier to determine where you stand in this table. This means that there are two ranges that determine what the multiplier would be. $\endgroup$ Jul 6 '18 at 15:29
  • 1
    $\begingroup$ @AndreyTsarev, if you'd like to clarify your question, please do so by editing your question. We want questions to stand on their own, so people don't have to read the comments to understand what you are asking. You might also specify in your question how you plan to evaluate answers (on what criteria or metrics). I hope you'll also explain what you mean by "determine where you stand"; I'm not sure I understand what that means. Thank you. $\endgroup$
    – D.W.
    Jul 6 '18 at 15:53
  • $\begingroup$ Sorry about that, updated question. Thanks! $\endgroup$ Jul 12 '18 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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