{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Array Interview Questions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 1:\n", "Given a N*N matrix, 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": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[3, 2, 1], [6, 5, 4], [9, 8, 7]]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def rotateMatrix(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", "rotateMatrix([[1, 2, 3],\n", " [4, 5, 6],\n", " [7, 8, 9]])\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "arr = [[1, 2, 3],\n", " [4, 5, 6],\n", " [7, 8, 9]]" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 3\n", "4 6\n", "7 9\n", "2 2\n", "5 5\n", "8 8\n" ] } ], "source": [ "for i in range(0, len(arr) - 1):\n", " x = 0\n", " while x <= len(arr) - 1:\n", " print(arr[x][i], arr[x][len(arr) - i - 1])\n", " x += 1" ] } ], "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 }