Conway's Game of Life - Amazon Top Interview Questions


Problem Statement :


You are given a two dimensional matrix where a 1 represents a live cell and a 0 represents a dead cell. A cell's (living or dead) neighbors are its immediate horizontal, vertical and diagonal cells. Compute the next state of the matrix using these rules:

Any living cell with two or three living neighbors lives.
Any dead cell with three living neighbors becomes a live cell.
All other cells die.

Constraints

n, m ≤ 500 where n and m are the number of rows and columns in matrix

Example 1

Input

matrix = [
    [1, 1, 1, 0],
    [0, 1, 0, 1],
    [0, 1, 0, 0],
    [1, 1, 0, 1]
]

Output

[
    [1, 1, 1, 0],
    [0, 0, 0, 0],
    [0, 1, 0, 0],
    [1, 1, 1, 0]
]



Solution :



title-img




                        Solution in C++ :

bool save(vector<vector<int>>& v, int row, int col) {
    return (v.size() > row && v[0].size() > col && row >= 0 && col >= 0);
}
vector<vector<int>> solve(vector<vector<int>>& v) {
    int n = v.size();
    int m = v[0].size();
    int p[] = {-1, -1, -1, 0, 0, 1, 1, 1};
    int q[] = {-1, 0, 1, -1, 1, -1, 0, 1};
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (v[i][j] > 0) {
                for (int k = 0; k < 8; k++) {
                    if (save(v, i + p[k], j + q[k]) && v[i + p[k]][j + q[k]] > 0) {
                        v[i][j]++;
                    }
                }
            } else {
                for (int k = 0; k < 8; k++) {
                    if (save(v, i + p[k], j + q[k]) && v[i + p[k]][j + q[k]] > 0) {
                        v[i][j]--;
                    }
                }
            }
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (v[i][j] > 0) {
                if (v[i][j] < 3)
                    v[i][j] = 0;
                else if (v[i][j] <= 4)
                    v[i][j] = 1;
                else if (v[i][j] > 4)
                    v[i][j] = 0;
            }
            if (v[i][j] <= 0) {
                if (v[i][j] == -3)
                    v[i][j] = 1;
                else
                    v[i][j] = 0;
            }
        }
    }
    return v;
}
                    




                        Solution in Python : 
                            
class Solution:
    def solve(self, matrix):
        if not matrix or not matrix[0]:
            return

        n, m = len(matrix), len(matrix[0])
        moves = [(1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)]

        for i in range(n):
            for j in range(m):
                living_count = 0

                for x, y in moves:
                    di = i + x
                    dj = j + y

                    if di >= 0 and di < n and dj >= 0 and dj < m and abs(matrix[di][dj]) == 1:
                        living_count += 1

                if matrix[i][j] == 1 and (living_count < 2 or living_count > 3):
                    matrix[i][j] = -1

                if matrix[i][j] == 0 and living_count == 3:
                    matrix[i][j] = 2

        for i in range(n):
            for j in range(m):
                if matrix[i][j] > 0:
                    matrix[i][j] = 1
                else:
                    matrix[i][j] = 0

        return matrix
                    


View More Similar Problems

Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →

Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

View Solution →

Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

View Solution →

Tree: Level Order Traversal

Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F

View Solution →

Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <

View Solution →

Tree: Huffman Decoding

Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t

View Solution →