Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

Each prisoner`s rank can be represented by a node and his friends can be depicted by adjoining nodes.

hashmap with key=prisoner : value = Danger rank
Do BFS
for each node hashmap[prisoner]= rank + rank of directly connected nodes

return hashmap

Now this will work only if graph is connected, which if we take kevin bacon`s consideration, every prisoner should be connected to every prisoner

- abhinav bansal February 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <vector>
#include <utility>
#include <string>

using namespace std;

class prison{

private:
	int rank;
	string name;
	int totalrank;
	std::vector<prison> friends;
public:
	void compute_rank();
	prison(std::string name,int,std::vector<prison>);
	//~prison();
	inline std::string getName(){return this->name;}
	inline int getRank(){return this->rank;}
	inline int getTotal(){return this->totalrank;}
	inline std::vector<prison> getFriends(){return this->friends;}
	void print();
	
	
};
prison::prison(std::string name,int rank,std::vector<prison> friends){
	this->name = name;
	this->friends = friends;
	this->rank = rank;
	
	
}
void prison::print(){
	cout<<"Prisoner Name: "<<this->name<<" Rank: "<< this->rank<<endl;
	cout<<"Prisoner Friends: \n"<<"Name\tRank: "<<endl;
	for(auto f : this->friends){
		cout<<f.name<<"\t"<< f.rank<<endl;
	}
	cout<<endl;
}

void prison::compute_rank(){
	this->totalrank = this->rank;
	for(auto e : friends){
		totalrank+=e.rank;
	}
	
}

void find_most_dangerous(std::vector<prison> p){
	std::pair<int,std::string> mdp;
	p[0].compute_rank();
	mdp = make_pair(p[0].getTotal(), p[0].getName());
	
	for(auto e : p){
		e.compute_rank();
		if(e.getTotal() > mdp.first){
			mdp = make_pair(e.getTotal(),e.getName()); 
		}else{
			continue;
		}
	}
	
	cout<<"Most Dangerous prisoner: " << mdp.second << " rank : "<<mdp.first<<endl;
}

int main(){
	std::vector<prison> b;
	prison a("A", 21, b);
	b.push_back(a);
	prison c("B", 44, b);
	b.push_back(c);
	prison d("D", 10, b);
	b.push_back(d);
	prison ds("DS", 10, b);
	for(auto f : b){
		f.print();
	}
	find_most_dangerous(b);
	return 0;

}

- Tinashe February 12, 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