1 assumption i've realized is that every employee should have a unique name.
@manofsteel972
Great logic man! I really think thats a great solution. One thing i need to mention is that your solution should iterate for every non-boss node. Coz it is possible for one boss to have more than one employees.
for e.g,
If we want to find the costocompany of A, then we have to sum up the salaries of B, D, C, E and A.Code:A | ----------------------------- | | B C | | D E
So just going through the path D-B-A, or E-C-A is not enough. We have to include both paths, ie, D-B-A AND E-C-A. So we have to start with every non-boss node, and move up higher to check if we encounter the given name, meanwhile summing up the salaries of every node coming in the path. If we encounter the given name, we add it costToCompany... else we will eventually reach the end, and it that case we will not add the sum to costToCompany, and move on to the next non-boss node to repeat the process. We will do this untill we have done this with every non-boss node. In the end, we will have our cost to the freakin company for that particular employee.
@Wizza
Thanks for your support. And your solution is so cool coz its so simple!!
@anon
I agree, it would make better sense to make classes they way you have. But the problem is given the way its given.
One more thing, i know in written tests theres nothing much you can do, but if such a problem is asked in an interview, is it better to give the solution as it is, or to say "it makes better sense to do it this way...". I guess i would answer the question as it it, then offer my suggestions to improve it. what do you all say???
@jmd15
Yeah, just need to add a bit and your solution is done.
And yeah, i was just taken aback that poeple would accuse me of getting my "homework" done, and asking me to shoot myself!! lol...Its ironic because the i dont get homework in the first place, and when way back i used to get homework, i never did it. Im a senior (ie, final year) CS student, and we dont even get assignments in c/c++ anymore. Everything is java now. We periodically get some assignments we have to complete as part of a group, and those are fairly big problems. But c/c++ knowledge is still required coz companies ask for it.
I have a few more similar thought/logic/design problems, if someone is interested. Dont need to post any code. I beleive programming is 99% thought and 1% syntax and code. I think everyone will benefit from discussing stuff like this.
If you dont want me post more problems, once again, tell me about it, and i wont. Either way, cheers!