Amazon | Phone Screen | Job Submission and Selection

Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2024-12-13Added By Anonymous
Source: Link

Company: Amazon
Role: sde i, sde ii
Interview Stage: Phone Screen
Location: unknown
Year: unknown

Original Post Content:

Q1. Job Submission and Selection

You are tasked with managing a set of jobs, each identified by a string ID and associated with a weight. Implement a system where you can submit jobs and select the job with the maximum weight. If a job with the same ID already exists, you should sum up its weight with the new weight provided.

```
#Link the Related Leetcode Question in Comments
```

Implement the following two methods:

1. submit(String jobID, int weight)
* Accepts a jobID (string) and weight (integer).
* If the job with the given jobID already exists, add the new weight to the existing weight.
* If the job does not exist, create a new job with the given jobID and weight.

2. select()
* Returns the jobID of the job with the maximum weight.
* If there are multiple jobs with the same maximum weight, return the one that was submitted last.
* If no jobs exist, return null.

Examples:

```
# Example 1
submit("A", 2)
submit("B", 6)
submit("C", 3)
submit("D", 4)
submit("A", 3)

select() # Returns "B"
select() # Returns "A"
select() # Returns "D"

submit("A", 2)
select() # Returns "C"
select() # Returns "A"
select() # Returns null

# Example 2
submit("X", 10) # Adds job "X" with weight 10
submit("Y", 5) # Adds job "Y" with weight 5
submit("Z", 7) # Adds job "Z" with weight 7
submit("X", 4) # Updates job "X" by adding weight 4 (total weight of X = 14)
submit("Y", 10) # Updates job "Y" by adding weight 10 (total weight of Y = 15)
submit("Z", 7) # Updates job "Z" by adding weight 2 (total weight of Z = 14)

select() # Returns "Y" weight is 15
select() # Returns "X" weight is 14 and was added first
select() # Returns "Z" weight is 14
select() # Returns null
select() # Returns null
```

Boilerplate Java Code with Class Job and Solution Class JobSolution

```
import java.util.HashMap;
import java.util.Map;

class Job {
String jobID;
int weight;

Job(String jobID, int weight) {
this.jobID = jobID;
this.weight = weight;
}
}

class JobSolution {
// Submit a job with the given ID and weight.
public void submit(String jobID, int weight) {
// Implement this method
}
// Select the job with the highest weight.
public String select() {
// Implement this method
return null;
}
}
```

```
#Link the Related Leetcode Question in Comments

```

Q2. ATOI Convert String to Integer
Convert String to Integer given very basic constraints that it will fit in the Integer boundaries. Could have negavtive numbers too.
Cannot use existing Parser like Integer.Parse(int)

Comments