{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Array Interview Questions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 1:\n", "Given a N*N matrix, write a function `mirrorMatrix` to mirror the matrix.\n", "- Input: [[1, 2, 3],\n", " [4, 5, 6],\n", " [7, 8, 9]]\n", " \n", "- Output: [[3, 2, 1],\n", " [6, 5, 4],\n", " [9, 8, 7]]" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[3, 2, 1], [6, 5, 4], [9, 8, 7]]" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def mirrorMatrix(arr):\n", " N = len(arr) - 1\n", " for i in range(0, N):\n", " x = 0\n", " while x <= N:\n", " arr[x][i], arr[x][N - i] = arr[x][N - i], arr[x][i]\n", " x += 1\n", " return arr\n", "\n", "mirrorMatrix([[1, 2, 3],\n", " [4, 5, 6],\n", " [7, 8, 9]])\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 2:\n", "Given a N*N matrix, write a function `rotateMatrix` to rotate the matrix by 90 degrees clockwise\n", "- Input: [[1, 2, 3],\n", " [4, 5, 6],\n", " [7, 8, 9]]\n", " \n", "- Output: [[7, 4, 1],\n", " [8, 5, 2],\n", " [9, 6, 3]]" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[7, 4, 1], [8, 5, 2], [9, 6, 3]]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def rotateMatrix(arr):\n", " N = len(arr) - 1\n", " for layer in range(N//2):\n", " first = layer\n", " last = N - layer\n", " for j in range(first, last):\n", " # Saving the top element.\n", " top = arr[layer][j]\n", " # Move the left element to top\n", " arr[layer][j] = arr[- j - 1][layer]\n", " # Move bottom element to left\n", " arr[-j - 1][layer] = arr[- layer - 1][- j - 1]\n", " # Move right to bottom\n", " arr[-layer - 1][-j -1] = arr[j][-layer - 1]\n", " # Move to the rjght\n", " arr[j][-layer - 1] = top\n", " return arr\n", "\n", "rotateMatrix([[1, 2, 3],\n", " [4, 5, 6],\n", " [7, 8, 9]])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 3:\n", "Given a 2D list, write a function `diagonalSum` to calculate the sum of the diagonal elements.\n", "- Input: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n", "- Output: 15" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "15" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def diagonalSum(arr):\n", " sum, i = 0, 0\n", " while i < len(arr):\n", " for element in arr:\n", " sum += element[i]\n", " i += 1\n", " return sum\n", "\n", "diagonalSum([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 4:\n", "Given a list, find out first and second highest value. Note: You cannot use `max()` or `sort()` functions.\n", "- Input: [5, 3, 1, 2, 4]\n", "- Output: [5, 4]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(5, 4)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def twoHighest(arr):\n", " first = arr[0]\n", " second = 0\n", " for i in range(1, len(arr)):\n", " if arr[i] > first:\n", " first = arr[i]\n", " elif arr[i] > second and arr[i] < first:\n", " second = arr[i]\n", " return (first, second)\n", "\n", "twoHighest([5, 3, 1, 2, 4])" ] } ], "metadata": { "interpreter": { "hash": "40d3a090f54c6569ab1632332b64b2c03c39dcf918b08424e98f38b5ae0af88f" }, "kernelspec": { "display_name": "Python 3.8.3 ('base')", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.3" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }