Pārlūkot izejas kodu

added few problems for arrays

Harsh Parikh 2 gadi atpakaļ
vecāks
revīzija
81aba3f859

+ 121 - 25
data_structures/arrays/interview_questions/array_interview_questions.ipynb

@@ -12,7 +12,7 @@
    "metadata": {},
    "source": [
     "### Problem 1:\n",
-    "Given a N*N matrix, mirror the matrix.\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",
@@ -24,7 +24,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 9,
+   "execution_count": 1,
    "metadata": {},
    "outputs": [
     {
@@ -33,13 +33,13 @@
        "[[3, 2, 1], [6, 5, 4], [9, 8, 7]]"
       ]
      },
-     "execution_count": 9,
+     "execution_count": 1,
      "metadata": {},
      "output_type": "execute_result"
     }
    ],
    "source": [
-    "def rotateMatrix(arr):\n",
+    "def mirrorMatrix(arr):\n",
     "    N = len(arr) - 1\n",
     "    for i in range(0, N):\n",
     "        x = 0\n",
@@ -48,46 +48,142 @@
     "            x += 1\n",
     "    return arr\n",
     "\n",
-    "rotateMatrix([[1, 2, 3],\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": 10,
+   "execution_count": 2,
    "metadata": {},
-   "outputs": [],
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "[[7, 4, 1], [8, 5, 2], [9, 6, 3]]"
+      ]
+     },
+     "execution_count": 2,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
    "source": [
-    "arr = [[1, 2, 3],\n",
+    "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]]"
+    "          [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": 13,
+   "execution_count": 3,
    "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"
-     ]
+     "data": {
+      "text/plain": [
+       "15"
+      ]
+     },
+     "execution_count": 3,
+     "metadata": {},
+     "output_type": "execute_result"
     }
    ],
    "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"
+    "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: [4, 5]"
+   ]
+  },
+  {
+   "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])"
    ]
   }
  ],