ADOBE Coding Question – Solved

11 Live
School renovation Given N classrooms in a school and each classroom has a capacity of A[i] students. Bob is a builder and follows the instructions of Alice. Alice gives Q instructions of the following types: - 1 L 0: Move L classrooms to the left - 2 R 0: Move R classrooms to the right - 3 X Y: Remove the next classroom and add two new classrooms of capacity X and Y respectively to the right of the current classroom. (After performing this operation, classroom numbers change accordingly) Note: The queries are always valid. Initially, Bob is in the first classroom. After performing all instructions from Alice, print the capacity of all classrooms from 1 to the total number of classrooms. Function description Complete the solve function. This function takes the following 2 parameters and returns the required answer. Parameters: - A: Represents a linear array denoting the capacity of classrooms in the old school - queries: Represents a 2D array denoting instructions given by Alice of the given types Input format for custom testing Note: Use this input format if you are testing against custom input or writing code in a language where we don't provide boilerplate code. - The first line contains two space-separated integers N and Q denoting the initial number of classrooms and the number of instructions. - The second line contains N space-separated integers denoting initial classroom capacities. - Next Q lines contain queries of the form: - 1 L 0 - 2 R 0 - 3 X Y Output format After performing all instructions from Alice, return the capacity of all classrooms from 1 to K (K = total number of classrooms in the renovated school). Sample input 5 4 1 2 1 4 5 2 1 0 1 1 1 1 2 0 3 5 2 Sample output 1 2 5 7 4 1 1 Explanation Bob moves 1 classroom right, so he is in the 2nd classroom now. He moves 1 classroom left, so he is back to the 1st classroom. He moves 2 classrooms left (stays at index 0 since it's the start). Then, from current position (0), he removes the next classroom (2nd) with capacity 2 and adds classrooms with capacities 5 and 7. Final classroom capacities: [1, 2, 5, 7, 4, 1, 1]

Asked in: ADOBE

Image of the Question

Question Image Question Image Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


class Node:
    def __init__(self, val):
        self.val = val
        self.prev = None
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To approach the "School Renovation" problem, the key is to carefully track Bob's position among classrooms and efficiently update the classroom list as per the instructions provided by Alice. The problem involves performing several types of operations that modify the classroom list dynamically, and after all instructions, we must output the final configuration of classroom capacities.

Step 1: Understand the Initial Setup
You start with N classrooms, each having some capacity A[i]. Bob begins at the first classroom (index 0 if zero-based indexing is used internally). The classrooms are arranged linearly, and Bob's position changes with move operations.

Step 2: Identify the Three Types of Instructions and Their Effects
- Type 1 (Move left): Bob moves L classrooms to the left. If Bob is at the start (index 0), he cannot move further left. This means Bob's position is the maximum of (current_position - L) and 0.
- Type 2 (Move right): Bob moves R classrooms to the right. Bob cannot move beyond the last classroom index (which changes if classrooms are added or removed). So Bob's position after moving right is the minimum of (current_position + R) and the last classroom's index.
- Type 3 (Modify classrooms): At Bob’s current position, remove the *next* classroom (so, the classroom immediately to the right of Bob's current position). Then add two new classrooms with capacities X and Y immediately to the right of Bob's current position. This increases the total number of classrooms by one since one classroom is removed and two are added. After this, the indices of classrooms to the right shift accordingly.

Step 3: Data Structure Considerations
Since classrooms can be removed and inserted anywhere (specifically, just after Bob’s current position), using a simple array and performing insertions/removals could lead to inefficient operations if implemented naively, especially for large N and Q.

- A linked list or a doubly linked list structure would be ideal because insertion and removal at any position can be done in O(1) time if you already have a reference (pointer) to the node. However, in some languages or constraints, linked lists can be tricky or inefficient in practice.
- Another approach is to use balanced data structures like balanced binary search trees or segment trees, but these may be overkill here given the constraints are not explicitly huge.
- Since we always insert or remove classrooms next to Bob’s current position, maintaining an index or pointer to Bob’s current classroom and the node next to it is crucial.

Step 4: Tracking Bob’s Position
Maintain Bob’s current position as an index or a pointer to a node (classroom) in the data structure.
- For move operations (left or right), simply adjust Bob’s position accordingly, making sure to clamp within valid boundaries (start or end of list).
- For modification operations, perform the removal of the next classroom and insert the two new classrooms immediately after Bob’s current position.

Step 5: Implementing the Modify Operation Carefully
- Locate the classroom next to Bob’s current position. Remove it from the data structure.
- Insert two new classrooms with capacities X and Y after Bob’s current position.
- The indices or pointers of the classrooms after the inserted nodes shift accordingly.
- Bob's position does not change due to this operation, only the list itself changes around him.

Step 6: Handling Indexing and Edge Cases
- Carefully handle edge cases, such as when Bob is at the last classroom and an instruction asks to remove the "next" classroom, which doesn’t exist. The problem states queries are always valid, so this might not happen.
- When Bob tries to move left beyond the first classroom, position stays at the first. Similarly, moving right beyond the last classroom stays at the last.
- After all instructions, the list of classrooms will have grown or shrunk due to the insertions/removals. Output the capacities in order.

Step 7: Efficiently Managing the Data
- Since modifications happen only at Bob’s current position plus one, you don't need to implement complex insertion logic for arbitrary positions.
- If you use a list or array, remember that insertions and removals are costly if done naively due to shifting elements.
- To optimize, consider data structures that support efficient insertion and removal at arbitrary points. For example, a doubly linked list or a balanced tree that maintains order.
- If the constraints are moderate, a linked list or a simulated linked list using arrays might be sufficient.

Step 8: Final Output
Once all Q instructions have been executed, iterate through the entire list of classrooms and print the capacities in order.

Summary of Thought Process:
- Keep track of Bob’s current classroom position.
- Carefully update position with move instructions, ensuring boundaries are respected.
- For modification instructions, remove the classroom immediately after Bob’s position and insert two new classrooms with given capacities.
- Use an appropriate data structure to support efficient insertions/removals next to Bob’s position.
- After processing all queries, output the capacities of the classrooms from the first to the last.
- Validate correctness by simulating operations carefully and ensuring indexing aligns with problem statements.

This approach balances correctness and efficiency by focusing on tracking Bob’s position precisely and managing the classroom list with suitable data structures for dynamic updates.
```


Related Questions