Browse Source

added array problems

Harsh Parikh 2 years ago
parent
commit
7fb63fdc45

+ 5 - 4
README.md

@@ -3,8 +3,9 @@
 ### Codes for data structures and algorithms in python.
 1. This is the repository for all the data structures and algorithms in python.
 1. The data structures list:
+   1. Array
    1. Hashmap
-   2. Stack
-   3. Queue
-   4. Linked List
-   5. Tree
+   1. Stack
+   1. Queue
+   1. Linked List
+   1. Tree

+ 37 - 0
data_structures/arrays/THEORY.md

@@ -0,0 +1,37 @@
+# Array
+
+### What is an Array:
+- Array is a collection of objects or things of a similar type.
+- Arrays are one of the most used data structures in the world of computer science.
+
+### Properties of an Array:
+- Array can only store data of the same type.
+- Elements of an array are located in a contiguous fashion.
+- Each element of an array has a unique index.
+- The size of an array is predefined and cannot be modified.
+
+### Types of Array:
+1. One Dimensionsal
+    - Example
+        ```python
+        array = [1, 2, 3, 4, 5]
+        ```
+2. Multi-Dimensional
+    - Example
+      ```python
+      array = [[1, 2 , 3],
+                [4, 5, 6]]
+      ```
+### Array operations.
+1. Create O(1)
+1. Insertion O(1)
+1. Traversal O(n), where n = array length
+1. Accessing an element O(1)
+1. Deletion O(1)
+
+`Please note, the time complexity mentioned above is for a One-Dimensional Array.`
+
+
+
+
+More about arrays can be found [here.](https://www.geeksforgeeks.org/introduction-to-arrays/)

+ 58 - 0
data_structures/arrays/interview_questions/array_interview_questions.ipynb

@@ -0,0 +1,58 @@
+{
+ "cells": [
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "# Array Interview Questions"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "### Problem 1:\n",
+    "Given a N*N matrix, 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": null,
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  }
+ ],
+ "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
+}

+ 149 - 0
data_structures/arrays/practice/arrays.ipynb

@@ -0,0 +1,149 @@
+{
+ "cells": [
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "## List Operations"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 1,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "1\n",
+      "2\n",
+      "3\n",
+      "4\n",
+      "5\n",
+      "6\n"
+     ]
+    }
+   ],
+   "source": [
+    "# Traversing a list \n",
+    "foo = [1, 2, 3, 4, 5, 6]\n",
+    "for element in foo:\n",
+    "    print(element)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 2,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "[1, 2, 3, 4, 5, 6, 7]\n"
+     ]
+    }
+   ],
+   "source": [
+    "# Insert an element\n",
+    "foo = [1, 2, 3, 4, 5, 6]\n",
+    "foo.append(7)\n",
+    "print(foo)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 3,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "[1, 2, 3, 4, 5]\n"
+     ]
+    }
+   ],
+   "source": [
+    "# Delete an element\n",
+    "foo = [1, 2, 3, 4, 5, 6]\n",
+    "foo.remove(6)\n",
+    "print(foo)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 4,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "[1, 2, 3, 4, 5]\n"
+     ]
+    }
+   ],
+   "source": [
+    "# Deleting an element using pop\n",
+    "foo = [1, 2, 3, 4, 5, 6]\n",
+    "foo.pop(5) # Here, the pop function takes an index instead of value.\n",
+    "print(foo)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 5,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "[1, 2, 3, 4, 5, 6]\n"
+     ]
+    }
+   ],
+   "source": [
+    "# Concating two arrays\n",
+    "foo_1 = [1, 2 ,3]\n",
+    "foo_2 = [4, 5, 6]\n",
+    "foo_3 = foo_1 + foo_2\n",
+    "print(foo_3)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  }
+ ],
+ "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
+}

+ 269 - 0
data_structures/arrays/practice/arrays_practice_problems.ipynb

@@ -0,0 +1,269 @@
+{
+ "cells": [
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "# Practice Problems"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "### Problem 1:\n",
+    "Given an integer array, find the missing number:\n",
+    "\n",
+    "- Input = [1, 2, 3, 4, 6, 7]\n",
+    "- Output = 5"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 1,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "5"
+      ]
+     },
+     "execution_count": 1,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "def missingNumber(arr): \n",
+    "    n = len(arr) + 1 #Since a number is missing, we need to add an extra element.\n",
+    "    theoritical_sum = (n * (n + 1))/2\n",
+    "    actual_sum = sum(arr)\n",
+    "    return int(theoritical_sum - actual_sum)\n",
+    "\n",
+    "missingNumber([1, 2, 3, 4, 6, 7])"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "### Problem 2:\n",
+    "Given an integer array, find the pairs whose sum is equal to a given number. \n",
+    "- Input: [1, 2, 3, 4, 5] target_sum = 7\n",
+    "- Output: (3, 4), (2, 5)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 2,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "[(5, 2), (2, 5), (3, 4), (4, 3)]"
+      ]
+     },
+     "execution_count": 2,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "# Method 1\n",
+    "def findSum(arr, target): # Time Complexity: O(n^2)\n",
+    "    results = []\n",
+    "    for i in arr:\n",
+    "        for j in arr:\n",
+    "            if i == j:\n",
+    "                continue\n",
+    "            if (i + j) == target:\n",
+    "                results.append(tuple([i, j]))\n",
+    "    \n",
+    "    return list(set(results))\n",
+    "\n",
+    "findSum([1, 2, 3, 4, 5], 7)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 3,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "[(2, 5), (3, 4)]"
+      ]
+     },
+     "execution_count": 3,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "# Method 2\n",
+    "def findSum(arr, target): # Time Complexity: O(log(n) + n) => O(n)\n",
+    "    arr.sort()\n",
+    "    results = []\n",
+    "    low = 0 \n",
+    "    high = len(arr) - 1\n",
+    "    while (low < high):\n",
+    "        if (arr[low] + arr[high]) == target:\n",
+    "            results.append(tuple([arr[low], arr[high]]))\n",
+    "        \n",
+    "        if (arr[low] + arr[high]) < target:\n",
+    "            low += 1\n",
+    "\n",
+    "        else:\n",
+    "            high -= 1\n",
+    "    return results\n",
+    "findSum([2, 1, 3, 4, 5], 7)"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "### Problem 3\n",
+    "Given an integer array of range in with length (n + 1), find the duplicate number.\n",
+    "- Input: [1, 2, 3, 4, 5, 5, 6]\n",
+    "- Output: 5"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 4,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "5"
+      ]
+     },
+     "execution_count": 4,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "# Method 1\n",
+    "def findDuplicate(arr): # Time complexity: O(n), Space complexity: O(n)\n",
+    "    data = {}\n",
+    "    for element in arr:\n",
+    "        if element not in data:\n",
+    "            data[element] = 0\n",
+    "        data[element] += 1\n",
+    "\n",
+    "    for key, value in data.items():\n",
+    "        if value > 1:\n",
+    "            return key\n",
+    "\n",
+    "findDuplicate([1, 2, 3, 4, 5, 5, 6])"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 5,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "5"
+      ]
+     },
+     "execution_count": 5,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "# Method 2\n",
+    "def findDuplicate(arr): # Time complexity: O(n), Space complexity: O(n)\n",
+    "    data = []\n",
+    "    for element in arr:\n",
+    "        if element in data:\n",
+    "            return element\n",
+    "        else:\n",
+    "            data.append(element)\n",
+    "\n",
+    "findDuplicate([1, 2, 3, 4, 5, 5, 6])"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "### Problem 4\n",
+    "Given two lists, check whether the lists are a permutation of each other. Note, you must not use the `.sort()` function.\n",
+    "- Input_1: [1, 2, 3, 4], [4, 3, 1, 2]\n",
+    "- Output_1: True\n",
+    "- Input_2: [\"a\", \"c\", \"b\"], [\"j\", \"c\", \"b\"]\n",
+    "- Output_2: False"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 6,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "True\n",
+      "False\n"
+     ]
+    }
+   ],
+   "source": [
+    "def isPermutation(list_1, list_2):\n",
+    "    if len(list_1) != len(list_2):\n",
+    "        return False\n",
+    "\n",
+    "    count = len([element for element in list_1 if element in list_2])\n",
+    "    if count == len(list_1):\n",
+    "        return True\n",
+    "    else:\n",
+    "        return False\n",
+    "print(isPermutation([1, 2, 3, 4], [4, 3, 1, 2]))\n",
+    "print(isPermutation([\"a\", \"c\", \"b\"], [\"j\", \"c\", \"b\"]))"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  }
+ ],
+ "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
+}