Grid Connections Amazon OA 2023

Grid Connections Amazon OA 2023 Solution

A supply chain manager at Amazon Logistics wants to determine the number of connections between warehouses, represented as nodes on a grid. A grid with m rows and n columns is used to form a cluster of nodes. If a point in the grid has a value of 1, then it represents a node.

Each node in the cluster has a level associated with it. A node located in the ith row of the grid is a level i node.

Here are the rules for creating a cluster:

  • Every node at a level connects to the next level that contains at least 1 node (i.e., every node at level i connects to all the nodes at level k where k > i and k is the first level after level i than contains at least one note).
  • When i reaches the last level in the grid, no more connections are possible.

Given such a grid, please help the supply chain manager by finding the number of connections present in the cluster.

Input

  • grid: the nodes grid

Output

the total number of connections

Examples

Example 1:

Input: 1grid = [[1, 1, 1], [0, 1, 0], [0, 0, 0], [1, 1, 0]]

Output: 5

Explanation:

image

There are a total of 3+2=5 connections.

Grid Connections Amazon OA Solution
Grid Connections Amazon OA Solution

SOLUTION

Program: Grid Connections Solution in Python

def gridOfNodes(self, intervals: list[list[int]]) -> int:
        connections = 0
        prevNodes = 1
        # print(len(intervals[0]))
        for i in range(len(intervals)):
            currNodeCount = 0
            for j in range(len(intervals[0])):
                if(intervals[i][j] == 1):
                    currNodeCount+= 1
            if i == 0:
                prevNodes = currNodeCount
                continue
            elif currNodeCount >= 1:
                connections += currNodeCount * prevNodes
            prevNodes = currNodeCount
        return connections

Program: Grid Connections Amazon OA Solution in Python

import collections
class Solution:
    def gridOfNodes(self, intervals: list[list[int]]) -> int:
        connections = 0
        prevNodes = intervals[0].count(1)
        for interval in intervals[1:]:
            currNodeCount = interval.count(1)
            if currNodeCount >= 1:
                connections += currNodeCount * prevNodes
                prevNodes = currNodeCount
        return connections

Amazon OA 2023 Questions with Solution

  1. Shopping Patterns Solution Amazon OA 2023
  2. Reorder Data in Log Files Solution Amazon OA 2023
  3. Top K Frequent Words Solution Amazon OA 2023
  4. Trees Height Solution Amazon OA SDE 2023
  5. Counting Binary Substrings Amazon OA 2023
  6. Shipment Imbalance Amazon OA 2023
  7. Max Profit Amazon OA 2023
  8. Find Lowest Price Amazon OA 2023
  9. Decode String Frequency Amazon OA 2023
  10. Simple Cipher Amazon OA 2023
  11. Valid Discount Coupons Amazon OA 2023 Solution
  12. Count Maximum Teams Amazon OA 2023
  13. Minimum Coin Flips Amazon OA 2023
  14. Max Average Stock Price Amazon OA 2023 Solution
  15. Robot Bounded In Circle Amazon OA 2023
  16. Shopping Options Amazon OA 2023 Solution
  17. Fill The Truck Maximum Units on a Truck Amazon OA Solution
  18. Maximize Score After N Operations Number Game Solution Amazon OA 2023
  19. Slowest Key Amazon OA 2023 Solution
  20. Five Star Seller Maximum Average Pass Ratio Amazon OA 2023
  21. Split String Into Unique Primes Amazon OA 2023 Solution
  22. Storage Optimization Amazon OA 2023 Solution
  23. Minimum Difficulty of a Job Schedule Amazon OA 2023 Solution
  24. Autoscale Policy Utilization Check Amazon OA 2023
  25. Optimal Utilization Solution Amazon OA 2023
  26. Merge Two Sorted Lists Solution Amazon OA 2023
  27. Two Sum Unique Pairs Solution Amazon OA 2023
  28. Amazon Music Pairs Amazon OA 2023 Solution
  29. Class Grouping Amazon OA 2023 Solution
  30. Find Max products Amazon OA 2023 Solution
  31. Get encrypted number Amazon OA 2023 Solution
  32. Find Total Imbalance Amazon OA 2023 Solution
  33. Find Total Power Amazon OA 2023 Solution