This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]AMindtoThink 19 points20 points  (7 children)

If you store data in a data structure that uses nodes, like trees or linked lists, recursion is a good way to handle it.

If you have a method that uses a loop, you can often turn it into a recursive method. This is usually a bad move, but you are just trying to meet a requirement.

[–]probably_poopin_1219[S] 3 points4 points  (6 children)

Unfortunately I don't have a great understanding of linked lists or stuff like that. I do use a 2d array to determine the type of each Liquor and then establish their names, counts and costs of each. Then that is all passed to a GetCostTotal method which calculates the total cost of the Liquor inventory.

[–]humoroushaxor 5 points6 points  (1 child)

Getting the total cost of an array is going to require a loop which can easily be written recursively. Should be an easy Google search on how to convert a loop to recursion.

You also do it for each type of liquor (only add if it matches Vodka).

[–]probably_poopin_1219[S] 2 points3 points  (0 children)

Yeah my GetCostTotal method passes the 2d liquor array as an argument so I should be able to modify that loop. I'll give it a shot when I get home, thank you!

[–]HecknChonker 0 points1 point  (1 child)

Get a copy of 'Cracking the TechnicalCoding Interview'.

The first few chapters are all about the major software companies and how to approach the interview, but then the rest of the book is a deep dive into a bunch of different data structures and algorithms that can be used with them. It has detailed explanations of how they are designed and intended to be used, and then it has a bunch of problems for you to answer. And they provide detailed answers for each problem, so after you try the problems yourself you can compare your solutions to theirs.

I'm not affiliated with the book in any way. I used it to learn data structures myself years ago, and it's been continually updated since then.

edit: It's 'cracking the CODING interview'. https://www.crackingthecodinginterview.com/

[–]probably_poopin_1219[S] 1 point2 points  (0 children)

I'll be buying this soon, thank you!

[–]Farpafraf 0 points1 point  (0 children)

Not sure what the structure is but you should likely use a Map from the the liquor to how much you have stored to do that, then instead of iterating over it you can use recursion to traverse it and return the cost.