Google Interview Question for Backend Developers

Country: United States

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

* Everyone pays/receive payment in bills (no change)
* the input is sorted

void calculatePeerPayments(int[] input){
	int total = 0;
	for(Integer payment : input){
		total += payment;
	int avg = total/input.length;

	int payer = 0;
	int payee = input.length - 1;
	int carryOver = 0;

	while(payer < payee) {
		int payment = avg - input[payer] - carryOver;
		int receive = input[payee] - avg + carryOver;
		if(payment > receive) {
			System.out.println("payer pays payee $" + receive);
			carryOver = payment - receive;
		} else if(payment == receive) {
			System.out.println("payer pays payee $" + receive);
			carryOver = 0;
		} else {
			System.out.println("payer pays payee $" + receive);
			carryOver = receive - payment;

- davie.lin76 January 15, 2018 | Flag Reply

Add a Comment

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


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


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