Click here to Skip to main content
15,867,750 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi Guys,

I recently had a coding test for a position of algorithm engineer in a company. They gave me the following problem. Although, I failed the coding challenge, but I would like to know the solution to the problem to see where did I go wrong?

Problem:

You live in a remote settlement where bread sellers come through periodically at irregular intervals. Whenever you buy fresh bread, it lasts for 30 days until it becomes too stale to eat. Your family eats one loaf of bread per day.

You are given a calendar of when the bread sellers will be visiting over the coming days and the price each bread seller will charge per loaf. You currently have 10 fresh loaves, and at the end of the calendar you'll get a bunch of free bread, so you won't need to have any left on hand.

Write a function that tells you how much bread to buy from each of the sellers

Input:

total_days - an integer, the number of days in the calendar until the free bread arrives


sellers, - a list of pairs of integers (day, price). Each pair represents one bread seller. The day is how many days from the start until the seller arrives. The price is the price to buy each loaf of bread from this seller, in pennies

Output:

purchases, a list of integers of the same length as sellers. Each integer is how many loaves you should buy from each seller Or None, if there is no solution that does not force your family to eat stale bread at some point
You should output the purchase plan that minimizes the total cost. In case of ties, output the plan that requires buying from the fewest number of different sellers, and within those choose the plan that buys more bread earlier

Requirements:

Please write a well formatted, easily readable python function calculate_purchasing_plan(total_days, sellers) in a file called
your_name.py that solves this problem using only the standard python (i.e. no packages
required).
Please include a comment at the top of the file with your name and email as well as a short
paragraph describing why the algorithm you’ve provided should work.

Example:

Python
calculate_purchasing_plan(60, [(10,200), (15,100), (35,500), (50,30)])
[5, 30, 5, 10]


I'd really appreciate if anyone can go through my code and tell me of any example which shows the code doesn't give the minimum cost.

What I have tried:

I have solved this problem, thank you very much everyone.
Posted
Updated 28-Feb-20 3:03am
v4
Comments
[no name] 10-Feb-20 20:42pm    
Maybe "crrnt_breads" instead "current_breads" did you in. Sometimes, that's all it takes if you're competing against others. No one gets points for being obtuse.

Quote:
I'd really appreciate if anyone could give a solution to it.


We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
 
Share this answer
 
Comments
nagpalh 10-Feb-20 12:11pm    
@OriginalGriff

Thanks for your comment. I didn't mean I need a solution, I have explained that I took this coding challenge and I did not pass the challenge. However, I was just told that my solution did not pass the threshold required. I have attached my solution, I just need people to look at it to point out some error or give a counter example where the algorithm doesn't work.

Thanks again.
OriginalGriff 10-Feb-20 12:22pm    
It's still your challenge, your "Homework" not ours!
So start testing what you have. See what it does. See what caused it to "not pass the threshold required" - because we have no idea if that was on style points, accuracy, performance, or some other metric. We don't even have a clue what metrics it is being tested against, or with what data ... so our saying "do this" probably wouldn't help it pass.
nagpalh 10-Feb-20 12:26pm    
@OriginalGriff

Thanks man. Appreciate it.
Quote:
but I would like to know the solution to the problem

There is about 1 solution per programmer
Quote:
where did I go wrong?

As programmer, your job is also to make sure that your code give correct solution. To do so, you have to build test datasets to cover different situations and check that result is the 1 expected.
Analyzing your code is is real work, basically, one have to reconstruct your algorithm from code.
Quote:
Please include a comment at the top of the file with your name and email as well as a short

I don't see those in your code.
Quote:
a short paragraph describing why the algorithm you’ve provided should work.

The comments in your code are simply repeating points of requirement, this is not an algorithm description.
An algorithm can be : backtracking exhaustive search, recursive or not.
 
Share this answer
 
v2
Well, there is a guy who is asking everybody to do this test because he is (supposedly) hiring people for his team. But when I did this test I never got an answer and the guy just disappeared silently. I think he has no money, and just whishiwashing. So, don't repeat the same mistake, don't program any tests before the interview and making sure there is someone who has funds for the project.

Find finished test with explanations on my GitHub, its in Rust:GitHub - nickhash/bbp: Rust test[^]
 
Share this answer
 
Comments
nagpalh 10-Apr-20 13:38pm    
Yeah, I asked them to give feedback at least, no response.

Your code is absolutely correct. You have considered all the possible scenarios. I saw that you have used a scenario of multiple bread-seller on one day, I didn't even think of that scenario.

I have a suggestion - it would be nice to output your result as dictionary of key=day-number and value=number-of-breads-bought.
Regards.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900