0% found this document useful (0 votes)
8 views

Java Test

Java Test All 5 PDFs submitted submitted

Uploaded by

kary.rock21
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Java Test

Java Test All 5 PDFs submitted submitted

Uploaded by

kary.rock21
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Java problems

1. Creating fair teams.


public int minDifference(int numTeams, int[] abilities) {

In this question you are passed an int that represents the number of teams we want to
form from a group of people. You are also passed an array of ints that represents the
ability scores of all of the individuals we want to assign to teams. (It could be athletic
ability, IQ, artistic ability, etc.) Abilities can be any integer value. A team's total
ability is simply the sum of the ability scores of the people assigned to that team.

The goal of this method is to find the minimum possible difference between the team
with the maximum total ability and the team with the minimum total ability. The
constraints are everyone (represented by a value in the array named abilities) must be
placed on a team and every team must have at least one member.

You must create the helper method. You must keep track of multiple things such as
which element in the abilities array you are currently considering (or which people
have already been assigned to a team), what the total ability score is for each team, as
well as some way of ensuring each team has at least one member. (You could simply
track the number of members per team.)

For example if we have the following abilities: [1, 2, 3, 4, 5, 6, 7] and want to form
three teams the min difference possible between the team with the maximum total
ability and the team with the minimum total ability is 1. Your program does not need
to show what the members of each team are but in this example the minimum possible
difference is created by having 2 teams with a total ability of 9 and 1 team with 10.
(min 9, max 10). There are multiple ways of doing this with these values one of which
is:
Team 1: 1 + 3 + 6 = 10
Team 2: 2 + 7 = 9
Team 3: 4 + 5 = 9

In this problem there is a lot to do when you reach the base case. You must ensure
every team has at least one person on it (or it is an invalid set up), and if it is a valid
set up find the min and max team ability scores. If the setup is invalid you must return
some value to indicate the set up was invalid or the largest possible difference..
(Integer,MAX_VALUE perhaps?) HINT: If not at the base case (all people
assigned to teams), handle one person. The choices for the current person are the
teams they could be assigned to.
2. MazeSolver
public int canEscapeMaze(char[][] maze)

Complete a method to determine if it is possible to escape a maze while collecting all


of the coins scattered throughout the maze.

The maze is represented with a rectangular 2d array of chars. Each element of the 2d
array is a cell and the maze has 6 types of cells:

• Starting cell: The Starting cell is marked with an S. There is only one Starting
cell in the maze. The starting cell becomes a green cell after we leave it.
• Impassable cells. Impassable cells are marked with an asterisk, *. We may not
enter impassable cells.
• Green cells. Green cells are marked initially with a G. When we leave a green
cell it becomes a yellow cells as described below.
• Yellow cells. Yellow cells are marked initially with a Y. When we leave a
yellow cell it becomes an impassable cell as described above.
• Money cells. Gold cells are marked with a $. Each money cell contains a single
gold coin we will pick up as we travel through the maze. There is no limit to
the number of coins we can pick up. When we leave a money cell it becomes a
yellow cell as described above.
• Exit cells. Exit cells are marked with an E. If we can reach an Exit cell we can
escape the maze. Exit cells may appear any where in the maze, not just on the
edges. There may me multiple exit cells in the maze. We we step into an Exit
cell, we exit the maze due to being teleported out. In other words we can't pass
through an exit cell to another cell in the maze.

The 2d array of chars represents a birds eye view of the maze. We can move up,
down, left, or right, but not diagonally in the maze. Your method shall return 2 if we
can reach an exit cell in the maze after collection all the gold coins present, a 1 if we
can reach an exit in the maze but without collecting all the gold coins, or a 0 if it is not
possible to exit the maze at all.
Consider the following simple maze:

G$SGE

We start at the cell marked with an S. We move to the left one spot to the cell with the
gold coin. We then move back to the right to our starting cell, to the right once more
to the regular cell and then right to the exit. Given this maze our method shall return
2.

Consider the following maze:

$Y$SGE

We can't escape this maze while collecting all the gold coins. We can move left to
pick up the first coin. When we move left again to the yellow cell. When we leave that
cell it becomes impassable. ($*YGGE) We can get to the gold coin on the far left, but
we are stuck there and can't exit the maze. We can exit the maze without picking up
all the coins so the method shall, in this case, return 1

You might also like