Question: Cognizant OnCampus OA 2026 | Technical Coding Assessment | Complete Solutions & Tips | Array Partitioning & 1D Coordinate Distances | Spring Campus Assessment
0
Entering edit mode

Question 1: Minimum Size

Problem Statement: You have some boxes and the weights of these boxes are given in an integer array A of size N. You are required to transport all these boxes using K available trucks.

All the boxes must be loaded onto the trucks, using the minimum possible capacity of each truck, in order to be transported. You have to ensure that the total weight of the boxes in each truck does not exceed the integer X, which represents the truck's maximum capacity.

Your task is to find and return an integer value representing the highest value of weights in the truck after optimally placing the boxes into each of the trucks while using each truck's minimum capacity. If transporting all the boxes is not possible, return -1.

Note: * You must load the boxes into the truck in the order they appear, i.e., only adjacent boxes can be loaded into a truck.

Input Specification:

  • input1: An integer value N, representing the number of boxes.
  • input2: An integer value K, representing the number of trucks.
  • input3: An integer value X, representing the maximum capacity of the trucks.
  • input4: An integer array A, where each element represents the weight of a box.

Output Specification:

  • Return an integer value representing the highest value of weights in the truck after optimally placing the boxes into each of the trucks while using the truck's minimum capacity. If transporting all the boxes is not possible, return -1.

Example 1:

  • input1: 4
  • input2: 3
  • input3: 5
  • input4: {2, 3, 1, 3}

 


Question 2: Nearest House Distance

Problem Statement: Consider the positive X-Axis as a straight road on which N houses are located. On a rainy day, M people are out on a walk. When the rain starts, each person runs towards the nearest house to them, so that they do not get wet. You are given the position of each house, the width of each house and the position of each person when the rain started, in separate integer arrays.

Your task is to find and return an integer value representing the sum of the distances travelled by all the people to reach the house nearest to them.

Note:

  • If a person is already inside the house, then consider the distance as 0.
  • A person can run in both forward and backward directions to reach the house nearest to them.
  • If a house is located at position X and its width is W, then the person is considered to be inside the house only if the position of the person lies in the range [X, X+W-1] (inclusive).

Input Specification:

  • input1: An integer value N, representing the number of houses.
  • input2: An integer array representing the position of each house on the road.
  • input3: An integer array representing the width of each house on the road.
  • input4: An integer value M, representing the number of the people on the road.
  • input5: An integer array representing the position of each person on the road.

Output Specification:

  • Return an integer value representing the sum of the distances travelled by all the people to reach the house nearest to them.

 

ADD COMMENTlink 6 hours ago admin 1.9k

Login before adding your answer.

Similar Posts
Loading Similar Posts