0% found this document useful (0 votes)
3 views4 pages

Java Coding Problems Solutions

The document presents two Java coding problems with solutions. The first problem focuses on maximizing unique service rates after one operation, while the second problem involves implementing a rate limiter using a sliding 60-second window. Each problem includes a detailed statement, approach, complexity analysis, and Java code implementation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Java Coding Problems Solutions

The document presents two Java coding problems with solutions. The first problem focuses on maximizing unique service rates after one operation, while the second problem involves implementing a rate limiter using a sliding 60-second window. Each problem includes a detailed statement, approach, complexity analysis, and Java code implementation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Coding Problems with Solutions

Problem 1: Maximize Unique Rates After One Operation

Problem 1: Maximize Unique Rates After One Operation

Statement:
You are given an array arr[] where arr[i] represents the current rate of service i.
You can perform at most one operation: select any subset of services and increase
or decrease their rate by 1. Each service must remain positive.

Goal: Find the maximum number of services that can have unique rates after the operation.

Approach:
Each rate can become one of {v-1, v, v+1}. We assign the smallest available
value for each occurrence to maximize distinct counts.

Complexity: O(n log n)

Java Code:
-------------------------------------------------
import [Link].*;

public class MaxUniqueRates {


public static int maxUniqueAfterOneOperation(int[] arr) {
Map<Integer, Integer> freq = new HashMap<>();
for (int v : arr) [Link](v, [Link](v, 0) + 1);

List<Integer> values = new ArrayList<>([Link]());


[Link](values);
Set<Integer> used = new HashSet<>();

for (int v : values) {


int count = [Link](v);
for (int i = 0; i < count; i++) {
if (v - 1 > 0 && ![Link](v - 1)) [Link](v - 1);
else if (![Link](v)) [Link](v);
else if (![Link](v + 1)) [Link](v + 1);
}
}
return [Link]();
}

public static void main(String[] args) {


[Link](maxUniqueAfterOneOperation(new int[]{1,2,2})); // 3
[Link](maxUniqueAfterOneOperation(new int[]{2,2,2})); // 3
[Link](maxUniqueAfterOneOperation(new int[]{1,1,1,2,2}));
}
}
-------------------------------------------------

Problem 2: Rate Limiter (Sliding 60s Window)

Problem 2: Rate Limiter with Sliding 60s Window

Statement:
You are given three inputs:
- List<Integer> users: user IDs of each request
- List<Integer> timestamps: corresponding timestamps (non-decreasing, in seconds)
- int k: allowed requests per 60-second sliding window

Return a List<Integer> with 1 if the request is allowed, 0 otherwise.

Approach:
For each user, keep a deque of timestamps of allowed requests.
For a new request at time t:
- Remove timestamps < t-59
- If [Link]() < k, allow and append timestamp
- Else, reject

Complexity: O(n) time, O(k * users) space.

Java Code:
-------------------------------------------------
import [Link].*;

public class RateLimiterList {


public static List<Integer> getAllowedRequests(List<Integer> users, List<Integer> timestamps, int
k) {
final int n = [Link]();
if (n == 0) return [Link]();
if ([Link]() != n) throw new IllegalArgumentException("Length mismatch");

final int WINDOW = 60;


Map<Integer, Deque<Integer>> map = new HashMap<>();
List<Integer> result = new ArrayList<>([Link](n, 0));

for (int i = 0; i < n; i++) {


int uid = [Link](i), ts = [Link](i);
Deque<Integer> dq = [Link](uid, id -> new ArrayDeque<>());
int earliestAllowed = ts - (WINDOW - 1);
while (![Link]() && [Link]() < earliestAllowed) [Link]();

if ([Link]() < k) {
[Link](i, 1);
[Link](ts);
}
}
return result;
}
public static void main(String[] args) {
List<Integer> users = [Link](1,1,1);
List<Integer> timestamps = [Link](6,10,65);
[Link](getAllowedRequests(users, timestamps, 2)); // [1,1,0]
}
}
-------------------------------------------------

You might also like