Data Structures & AlgorithmsModerateGATE CS · TCS NQT
In a grid, 2 marks a rotten orange, 1 a fresh one and 0 an empty cell. Each minute every fresh orange adjacent (4 directions) to a rotten one becomes rotten. The grid is [[2,1,1],[1,1,0],[0,1,1]]. How many minutes until no fresh orange remains?
- A. 5
- B. 3
- C. 4
- D. 6
Correct answer: C. 4
A multi-source breadth-first search starts from all rotten oranges at once; the number of BFS layers that infect something is the answer.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- The 0/1 grid [[1,1,0],[0,1,0],[1,0,1]] is given. Two 1-cells belong to the same island if they are adjacent horizontally or vertically…
- Two numbers are stored as linked lists of digits, least significant digit first: 9 -> 9 -> 9 and 1. Their sum is added digit by digit with…
- How many index pairs i < j in [3, 4, 2, 6, 1, 2, 12, 1] have a[i] a[j] = 12?
- How many distinct strings of length 3 are palindromes that occur as subsequences of abcab (of the form x y x with y different from x)?
- What is the length of the longest palindromic subsequence of the string leetcode?
- How many contiguous non-empty substrings of aaabbbaa consist of a single repeated letter?