Recruitment paper question

This is a discussion on Recruitment paper question within the C++ Programming forums, part of the General Programming Boards category; Hi all, This was a question in of the written exams conducted by a well known software firm at our ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    5

    Recruitment paper question

    Hi all,

    This was a question in of the written exams conducted by a well known software firm at our college. I just could'nt do it. Please try to solve and post the solution...


    A company consists of employees and their bosses.Each employee reports to one boss and each boss may have a no. of employees working under him. The cost to the compnay of an employee is his salary plus the salary of people working under him ,if he is a boss.In case he is not a boss to any other employee then his cost to the company is just his salary.

    The following class is given:
    Code:
    Class employee
    {
    name
    isboss;
    bossname;
    salary;
    getsalary();
    }
    Write a function float costothecompany(char *name) that accepts employee name as input and returns his costtothecompany as output.Make neceesary assumptions.


    Maybe im not making some necessery assumptions, thats why im not able to solve the problem.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > Please try to solve and post the solution...
    What for?
    For you to just hand over to the recruiter?
    Seems like just another "do my homework" for me post.

    > Maybe im not making some necessery assumptions
    Well how about you being by stating your assumptions, just to show that you've actually tried to the problem then.

    > The following class is given:
    I see no types, does that mean we're free to choose types?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Yeah, well it sounds as if your chance to complete that is over so I'll post how I would do it. The only assumptions I'm making is that they have all their employees(of class employee) stored in a big array called employees AND that they have an integer called max_employees that has the number of employees they have stored in it. Now, I have had no actually teaching on programming so don't expect top quality, I'm all self-taught through books and the internet. Although I may be self-taught, this was quite an easy assignment, that is if I understood you correctly. I tried to comment decently on it too. Let me know if this is what you are/were talking about. Attached.

    EDIT:
    By the way, if that assignment is still running and you take and use my solution, please shoot yourself. Right now, do it. I don't want that company thinking you have any clue what you're doing. Just think of the rage you'd instill in your fellow programmers if you were to be hired along with people who knew what they're doing. If that were to happen, methinks you'd get shot one way or another.
    Attached Files Attached Files
    Last edited by jmd15; 05-29-2006 at 01:22 PM.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,435
    No one gets hired simply by solving one problem. And companies don't recruit like that. Especially considering that was such an easy question. This is probably a school assignment you just solved for him or her.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    5
    Hey all,

    First of all, this problem is not an assisgnement or anything. It really was a problem that was given to our seniors last year when they came to our college. This year they are coming to our college.

    Just like its usefull to prepare for the SATs by doing questions that previously actually appeared on the SATs, it is better to prepare for these tests if you solve the previous years tests. These companies are not crazy, and never repeat the problems they have given previously. So im certain that this problem wont come this time around (or probably ever again in the future). However, a problem could come along the lines of this one. So if one knows the LOGIC of solving such a question, he can then easily solve other questions similar in nature to this.


    The main problem is that they said that we can assume anything, and they arnt gonna answer any doubts on this problem.

    I guess we are allowed to choose types.

    However, looking at the problem, name and bossname will be char, isboss probably boolean, salary as float...

    The problem im having is that this whole department can be thought of as a tree. If we make multiple objects of the employee class to represent each employee, then how to iterate through these objects???

    Well, I just realized i could make an array of objects (which jmd15 has also done), so the above doubt of mine is solved.

    Also each employee has a bossname, but no child name. So we can only see our parent (boss), and not our children (subordinates). So we can make a function in which it will display the salary if isboss is false...

    otherwise if isboss is true then what?? How do we determine which employees are the given person's subordinates??? We can't see our children, we can only see our parent...

    It may be the case that an employee is a boss that has subordinates who are also bosses. How do we traverse that tree???

    I hope my doubts are clear.

    I dont want the exact solution, i just want to know how to approach the problem.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,657
    This is a mental excercise for problem solving. There was no reason for you to assume any particular data structure (like an array) when you implemented employee. They provided an employee implementation for you, even. You're thinking was also correct. You could have built a tree of employees and traversed through it, thus solving your problem, yet you didn't for some reason. There was no "correct" solution. You just needed to solve a problem.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    5
    Thanks for the reply, Citizen.

    Im glad to know to know that at least my thinking wasnt way off the mark.

    Now i can use a tree. The problem is that each node will not know who it's children are (as we only name the name of our boss, "bossname". we are not provided with childname[], or something, which will let us identify the children of the node).

    So in the end we'll end up with a tree with all nodes pointing to the node above them...there will be no nodes pointing to its children.

    So top-down traversal is not possible i think. If we follow a bottom-up approach, then should i use level-order search??? How should i traverse the tree???

  8. #8
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    @ Mario F
    Of course they wouldn't hire you if you could answer one question, however some companies may give surveys of some sort to test the average knowledge of students coming from a certain college. Or perhaps to scout for possible employees.

    @ hungryfoolish
    Look at my code, I sort through all the employees checking their BOSSNAMES against the name of the employee we are checking. If the bossname matches the employee name then the employee in question's "child" is the person with the bossname that matches his own. Here I'll try to make it real simple. If bossname equals name, then name is the boss of the person who has them for a bossname. So, I checked through the employees comparing the employees bossname with the name of the guy we're checking and if they match then the guy we're checking is the boss of the employee who we compared with. Urgh, just look at the part in my code where I did that. Oh, and I'm glad to hear that this isn't an assignment or anything because cheating your way through school leads to problems later.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  9. #9
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Quote Originally Posted by Salem
    Seems like just another "do my homework" for me post.

    except you get an A for turning in your homework, not a possible position at a company. Funny if he gave us all the firm's info etc so we could send it to them.

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    5

    Reply

    @MarioF

    If its such an easy problem, then why dont you post the logic of solving such a problem. And yes, you're right. No one will get hired coz they solved just one problem. But if you think that they ask you to write thousands of lines of code in test and interviews, then you're wrong. Sometimes they dont even ask code, they might give you a puzzle to solve.


    @jmd15

    Man, i looked at your solution. Your code is only valid in case of a boss having non-boss employees. But soppose, you have an boss (mark) who had a employee who is also a boss (steve) who has a non-boss employee(gwen).

    you cannot calculate the costocompany() of mark correctly. According to your code, if i try to calculate the costocompany of mark, it will only return the sum of salaries of steve and mark. It will not include the salary of gwen.


    I think you're almost there, but what im thinking is to use some sort of recursive function similar to your function. Im trying to think what exactly that recursive solution could be....


    and please, cut it out with the homework thing. if you think its homework, then dont post post anything. I never wanted exact code. I never even wanted any kind of code. I just wanted the approach to solving it. I was just frustrated at not solving this one, so i thought maybe a fresh mind would, and thats why i posted this problem here. If i knew you guys would bust my nuts so much, i never would have posted it in the first place.

  11. #11
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,435
    Quote Originally Posted by hungryfoolish
    If its such an easy problem, then why dont you post the logic of solving such a problem.
    Because, first:
    I always read the rules before I post my very first question on any forum.

    Second:
    Because.

  12. #12
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    Just to give my 2 cents. First you need to traverse the list to find the boss name. With the class you could make the Bossname a vector of pointers. The isboss being a bool. If true then you know that there are at least 1 or more employees. In the function save the pointer to the current node if it matches the name of the employee you are getting the cost for. Now you need to get to cost of this employee. So now it doesn't matter the names, you just need to loop through everyone summing up thier salaries. So test to see if person is a boss or not. If the person is a boss save the pointer to the current person. follow the pointer for each employee testing if that person is a boss and repeat for each when you get to an employee you are at the end of the branch and you back up to the saved node to follow the next branch down to its end.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  13. #13
    Registered User
    Join Date
    May 2006
    Posts
    20
    It would be simpler to have
    Code:
    Class employee
    {
    char name[20];
    bool isboss;
    char bossname[20];
    float salary;
    float getsalary(){
       int salarytotal;
       if (!isboss)
       salarytotal=salary;
       else{for(int counter=0;counter<arraysize;counter++){
           if (ppl[counter].bossname=name)
           salarytotal+=ppl[counter].getsalary();
           }
       }
       return (salarytotal);
            }
       }ppl [#ofppl];
    }
    Good on you hungryfoolish, its always nice to get a good thought problem.
    These guys are being a little harsh on you but anyone who can tell how easy it is, would be more then happy to help you!

  14. #14
    The larch
    Join Date
    May 2006
    Posts
    3,573
    One thing doesn't make sense. We're not talking about the salary, but the cost of an employee to the company. The employee class should not have the getMyCostToCompany member, because no employee should be aware of their coworkers' wages.

    Therefore there should be another class (Company), which has an array of workers and also the method to find everyone's cost. The employee could be its friend (the company should know everything about the worker).
    Then you could use a recursive function to return everyone's cost.

    (It would also make sense if each boss actually kept a list of all people working directly under them. Now you'd have to loop through all the workers of the company - probably several times.)

    So here is what I would do (using strings instead of char arrays)

    Code:
    class Company;
    class Employee
    {
        private:
            std::string name, bossname;
            float salary;
            bool isBoss;
            friend class Company;
        public:
            Employee(std::string x, std::string y, float n, bool a):
                name(x), bossname(y), salary(n), isBoss(a) {}
    };
    
    class Company
    {
         private:
             std::vector<Employee> v;
         public:
             void addEmployee(Employee e) {v.push_back(e);}
             float getCost(Employee& s)
             {
                float result = s.salary;
                if (s.isBoss)
                    for (int i = 0; i < v.size(); i++)
                        if (v[i].bossname == s.name)
                            result += getCost(v[i]);
                return result;
            }
    };
    Last edited by anon; 05-31-2006 at 10:15 AM.

  15. #15
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Lol, now this is getting blown out of proportion. Yes I do admit that I didn't think of the underlings being a boss as well. You can just tack onto my code to fix that, by checking if the underlings are bosses and if they are then check their underlings until you get down to the grunts and then add up all the acquired salaries. Just add onto my code, and btw don't get mad about the homework thing because:
    1) It's a rule
    2) We have many people trying to get us to do their work
    3) We hate those people
    4) You need to be able to take critiscism and/or questions
    5) We are not REQUIRED to help
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help with rock paper scissors program
    By Mshock in forum C Programming
    Replies: 3
    Last Post: 04-22-2006, 07:44 AM
  2. PRS Help
    By SpudNuts in forum C Programming
    Replies: 10
    Last Post: 08-07-2005, 01:14 PM
  3. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  4. Beginner's Language Paper
    By Thantos in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 01-04-2004, 04:48 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21