Prefix & Suffix Sum Assesment
Prefix & Suffix Sum Assesment
Instructions:
Tool used: VSCode or Intellij Idea with proper environment setup
● Internet Usage:
○ Ensure that the internet is closed during the assessment. The
assessment will be conducted offline using an IDE.
○ Use of any online resources is prohibited.
● Mobile Phones:
○ Switch off your mobile phone.
○ Place your mobile phone inside your bag, and keep your bag outside
the assessment venue.
● Items to Carry:
○ Bring your laptop and charger.
○ Carry a pen, water bottle, and A4 sheets for dry run submission.
Marking scheme:
In a small village, there’s a legendary seesaw located at the center of the town. The
villagers believe that if someone can balance the seesaw perfectly with their weights,
they will receive immense wisdom and clarity in decision-making.
One day, a curious traveler named Alex arrives in the village. He is given an array of
weights, representing villagers lined up on the seesaw. Alex’s task is to find the
middleIndex, where the seesaw is perfectly balanced. The rules for balancing the
seesaw are:
1. The sum of weights on the left side of the middleIndex must equal the sum of
weights on the right side.
2. If Alex considers the first position (index 0), the left side has no weights, so the
sum is 0.
3. Similarly, if Alex considers the last position (index n−1n - 1n−1), the right side
sum is 0.
Alex wants to find the leftmost middleIndex where the seesaw balances. If no such
index exists, he will consider the task impossible and return -1.
Input:
Output:
● If Alex finds the seesaw's balance point, he returns the leftmost middleIndex.
● If no such index exists, he returns -1.
Example 1:
Input:
nums = [2, 3, -1, 8, 4]
Output:
3
Explanation:
Example 2:
Input:
nums = [1, -1, 4]
Output:
2
Explanation:
Example 3:
Input:
nums = [2, 5]
Output:
-1
Explanation:
Constraints:
1. 1≤nums.length≤1000
2. −1000≤nums[i]≤1000
Problem 2:
In the bustling town of Flytown, a new airline company has introduced a unique booking
system to manage its flight reservations. The town's people, excited about the new
service, start reserving seats for different flights. However, the airline faces a small
challenge—they need to calculate the total number of seats reserved for each flight
efficiently.
The airline now needs to calculate the total seats reserved for each flight.
Input:
Output:
Example 1:
Input:
n=5
bookings=[[1,2,10],[2,3,20],[2,5,25]]
Output:
[10,55,45,25,25]
Explanation:
Example 2:
Input:
n=3
bookings=[[1,3,5],[2,2,10]]
Output:
[5,15,55]
Explanation:
Constraints:
bookings[i].length == 3
Calculate the sum of the elements of matrix inside the rectangle defined by its upper left
corner (row1, col1) and lower right corner (row2, col2).
NumMatrix(int[][] matrix) Initializes the object with the integer matrix matrix.
int sumRegion(int row1, int col1, int row2, int col2) Returns the sum of the elements of
matrix inside the rectangle defined by its upper left corner (row1, col1) and lower right
corner (row2, col2).
You must design an algorithm where sumRegion works on O(1) time complexity.
Example 1:
Input
[[[[3, 0, 1, 4, 2], [5, 6, 3, 2, 1], [1, 2, 0, 1, 5], [4, 1, 0, 1, 7], [1, 0, 3, 0, 5]]], [2, 1, 4, 3], [1,
1, 2, 2], [1, 2, 2, 4]]
Output
Explanation
NumMatrix numMatrix = new NumMatrix([[3, 0, 1, 4, 2], [5, 6, 3, 2, 1], [1, 2, 0, 1, 5], [4,
1, 0, 1, 7], [1, 0, 3, 0, 5]]);
Constraints:
m == matrix.length
n == matrix[i].length
class NumMatrix {
public int sumRegion(int row1, int col1, int row2, int col2) {
/**
*/
Please implement the complete code in your IDE, ensuring it includes your best input-output
format. Use the provided code structure for this problem and include the main function to
execute the program directly from your IDE.