Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
0
of 0 vote

First attempt (unoptimized) using STL libraries. Note that the prisoner with the highest danger rank is the last element in the array. If you want it to be the first, either implement the comparer as a > (Greater), or use a list and reverse it at the end.

#include <vector>
#include <iostream>
#include <ctime>
#include <algorithm>
#include <functional>
using namespace std;

#define MAX_INMATES 50
#define MAX_DANGER_VALUE 15

class Prisoner
{
private:
    unsigned int idNumber;
    unsigned int dangerValue;
    unsigned int dangerRank;
    vector<unsigned int> friendIds;

public:
    Prisoner()
    {
        idNumber = 0;
        dangerValue = 0;
        dangerRank = 0;
        friendIds.clear();
    }

    inline void SetPrisonerIdNumber(unsigned int id) { idNumber = id; }
    inline void SetDangerValue(unsigned int value) { dangerValue = value; }
    inline void SetDangerRank(unsigned int rank) { dangerRank = rank; }
    inline unsigned int GetPrisonerIdNumber(void) const { return idNumber; }
    inline unsigned int GetDangerValue(void) const { return dangerValue; }
    inline unsigned int GetDangerRank(void) const { return dangerRank; }
    inline void AddFriendId(unsigned int id) { friendIds.push_back(id); }
    inline unsigned int GetNumberOfFriends(void) const { return static_cast<unsigned int>(friendIds.size()); }

    unsigned int CalculateDangerRank(vector<Prisoner>& inmates)
    {
        unsigned int rank = dangerValue;
        
        unsigned int numFriends = static_cast<unsigned int>(friendIds.size());
        unsigned int numInmates = static_cast<unsigned int>(inmates.size());
        for (unsigned int friendIndex = 0; friendIndex < numFriends; ++friendIndex)
        {
            unsigned int friendIdNumber = friendIds[friendIndex];
            for (unsigned int inmateIndex = 0; inmateIndex < numInmates; ++inmateIndex)
            {
                if (friendIdNumber == inmates[inmateIndex].GetPrisonerIdNumber())
                {
                    unsigned int friendDangerValue = inmates[inmateIndex].GetDangerValue();
                    rank += friendDangerValue;
                }
            }
        }

        return rank;
    }
};

void CreateMasterPrisonerList(vector<Prisoner>& inmates)
{
    inmates.clear();

    unsigned int numInmatesToCreate = rand() % MAX_INMATES + 1;
    for (unsigned int inmate = 0; inmate < numInmatesToCreate; ++inmate)
    {
        Prisoner newInmate;
        newInmate.SetPrisonerIdNumber(rand());
        newInmate.SetDangerValue(rand() % MAX_DANGER_VALUE);

        inmates.push_back(newInmate);
    }
}

void AssignFriends(Prisoner& inmate, unsigned int numFriends, vector<Prisoner>& inmates)
{
    // get the interval between each friend
    unsigned int numInmates = static_cast<unsigned int>(inmates.size());
    unsigned int interval = static_cast<unsigned int>(numInmates / numFriends);
    unsigned int offset = rand() % 5;

    for (unsigned int index = offset; index < numInmates; index += interval)
    {
        inmate.AddFriendId(inmates[index].GetPrisonerIdNumber());
    }

    inmate.SetDangerRank(inmate.CalculateDangerRank(inmates));
}

template<class T = Prisoner>
struct LessComparer
{
    bool operator() (const T& a, const T& b) const
    {
        return a.GetDangerRank() < b.GetDangerRank();
    }
};

void SortInmatesByDangerRank(vector<Prisoner>& inmates)
{
    // heap sort
    make_heap(inmates.begin(), inmates.end(), LessComparer<>());
    sort_heap(inmates.begin(), inmates.end(), LessComparer<>());
}

int main(void)
{
    srand(static_cast<unsigned int>(time(0)));
    vector<Prisoner> masterInmateList;

    cout << "PRISONER DANGER RANK TEST PROGRAM\n";
    cout << "=================================\n\n";
    cout << "Creating master prisoner list...\n";

    CreateMasterPrisonerList(masterInmateList);
    unsigned int numPrisoners = static_cast<unsigned int>(masterInmateList.size());
    cout << numPrisoners << " inmates registered.\n\n";
    
    cout << "Calculating inmate relationship...\n";
    for (unsigned int inmate = 0; inmate < numPrisoners; ++inmate)
    {
        unsigned int numFriendsToAssign = rand() % numPrisoners + 1;
        AssignFriends(masterInmateList[inmate], numFriendsToAssign, masterInmateList);
    }
    cout << "Inmate relationships mapped.\n\n";

    cout << "Sorting inmate register by danger value...\n";
    SortInmatesByDangerRank(masterInmateList);
    cout << "Inmate register sorted.\n\n";
    cout << "Inmates\n";
    
    for (unsigned int inmate = 0; inmate < numPrisoners; ++inmate)
    {
        cout << "\tID: " << masterInmateList[inmate].GetPrisonerIdNumber() << "\n";
        cout << "\tNumber Friends: " << masterInmateList[inmate].GetNumberOfFriends() << "\n";
        cout << "\tDanger Value: " << masterInmateList[inmate].GetDangerValue() << "\n";
        cout << "\tDanger Rank: " << masterInmateList[inmate].GetDangerRank() << "\n\n";
    }

    cout << "\n";
    system("Pause");
    return(EXIT_SUCCESS);
}

- Seth Jacobson February 09, 2015 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More