Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UofT-DSI | Algorithms and data structures - Assignment 2 #31

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions 01_materials/slides/1_motivation_big_o.ipynb

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,392 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Practice Interview\n",
"\n",
"## Objective\n",
"\n",
"_*The partner assignment aims to provide participants with the opportunity to practice coding in an interview context. You will analyze your partner's Assignment 1. Moreover, code reviews are common practice in a software development team. This assignment should give you a taste of the code review process.*_\n",
"\n",
"## Group Size\n",
"\n",
"Each group should have 2 people. You will be assigned a partner\n",
"\n",
"## Part 1:\n",
"\n",
"You and your partner must share each other's Assignment 1 submission."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## Part 2:\n",
"\n",
"Create a Jupyter Notebook, create 6 of the following headings, and complete the following for your partner's assignment 1:\n",
"\n",
"- Paraphrase the problem in your own words.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Your answer here\n",
"A list of integers is given, \n",
"including some numbers within the range from 0 to n but not necessarily all numbers in this range.\n",
"The task identifies which numbers within the range [0, n] \n",
"are missing from the list. If all numbers are present, return -1."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"- Create 1 new example that demonstrates you understand the problem. Trace/walkthrough 1 example that your partner made and explain it.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Your answer here\n",
"Example.\n",
"Input: lst = [1, 3, 5, 7]\n",
"Output: [0, 2, 4, 6]\n",
"Explanation: The numbers in the range [0, 7] are 0, 1, 2, 3, 4, 5, 6, and 7. The numbers 0, 2, 4, and 6 are missing from the list."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"- Copy the solution your partner wrote. \n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Your answer here\n",
"def missing_num(nums: list) -> int:\n",
" if len(nums) == 0:\n",
" return -1\n",
" min_num = min(nums)\n",
" max_num = max(nums)\n",
" miss_list = []\n",
"\n",
" for i in range(min_num, max_num):\n",
" if i not in nums:\n",
" miss_list.append(i)\n",
"\n",
" len(miss_list) == 0\n",
" return -1 if len(miss_list) == 0 else miss_list"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"- Explain why their solution works in your own words.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Your answer here\n",
"The provided solution aims to find the missing numbers in the range [0, n] for a given list of integers. \n",
"Here's a step-by-step explanation of why this solution works:\n",
"1. Initial Check for Empty List:\n",
"\t\n",
"\tif len(nums) == 0:\n",
" return -1\n",
"\n",
"- If the input list nums is empty, \n",
"the function immediately returns -1 because \n",
"there are no numbers to analyze or find missing values from.\n",
"2. Determine the Range:\n",
"\n",
"\tmin_num = min(nums)\n",
"\tmax_num = max(nums)\n",
"\n",
"- The function determines the smallest (min_num) and largest (max_num) numbers in the list nums. \n",
"However, since we are supposed to find missing numbers in the range [0, n], \n",
"this step is somewhat redundant because the range \n",
"should always start from 0 and go to the maximum number present in the list.\n",
"3. Initialize the Missing List:\n",
"\n",
"\tmiss_list = []\n",
"\n",
"4. Find Missing Numbers:\n",
"\n",
"\tfor i in range(min_num, max_num):\n",
" if i not in nums:\n",
" miss_list.append(i)\n",
"\t\t\n",
"- The function iterates through the range from min_num to max_num (exclusive) \n",
"and checks if each number i is present in the list nums.\n",
"- If a number i is not found in nums, it is appended to the miss_list.\n",
"5. Check for No Missing Numbers:\n",
"\t\n",
"\tlen(miss_list) == 0\n",
"\treturn -1 if len(miss_list) == 0 else miss_list\n",
"\n",
"- The function checks if the miss_list is empty.\n",
"- If there are no missing numbers, it returns -1.\n",
"- Otherwise, it returns the list of missing numbers.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"- Explain the problem’s time and space complexity in your own words.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Your answer here\n",
"Time Complexity\n",
"1. Initial Check for Empty List:\n",
"\tif len(nums) == 0:\n",
"\t\treturn -1\n",
"-This step checks if the list is empty, which takes 𝑂(1) time.\n",
"2. Finding Minimum and Maximum Values:\n",
"\tmin_num = min(nums)\n",
"\tmax_num = max(nums)\n",
"- Finding the minimum value in the list takes 𝑂(𝑛) time.\n",
"- Finding the maximum value in the list also takes 𝑂(𝑛) time.\n",
"- Thus, this step takes a total of 𝑂(𝑛+𝑛)=𝑂(2𝑛)=𝑂(𝑛) time.\n",
"3. Iterating Through the Range and Checking for Missing Numbers:\n",
"\tfor i in range(min_num, max_num):\n",
"\t\tif i not in nums:\n",
"\t\t\tmiss_list.append(i)\n",
"- The for loop iterates from min_num to max_num (exclusive), which can take up to 𝑂(𝑛) iterations.\n",
"- For each iteration, the if i not in nums statement checks if i is in the list nums, \n",
"which takes 𝑂(𝑛) time because checking membership in a list is a linear operation.\n",
"- Therefore, the nested operations result in 𝑂(𝑛×𝑛)=𝑂(𝑛2) time complexity for this step.\n",
"4. Checking if miss_list is Empty and Returning Result:\n",
"\tlen(miss_list) == 0\n",
"\treturn -1 if len(miss_list) == 0 else miss_list\n",
"- Checking the length of miss_list and the return statement both take 𝑂(1) time.\n",
"\n",
"Overall Time Complexity: The dominant term is the nested loop which gives us 𝑂(𝑛2) time complexity.\n",
"\n",
"Space Complexity\n",
"1. Space for Minimum and Maximum Values:\n",
"\tmin_num = min(nums)\n",
"\tmax_num = max(nums)\n",
"- Storing min_num and max_num takes 𝑂(1) space each.\n",
"2. Space for miss_list:\n",
"\tmiss_list = []\n",
"- The miss_list can grow up to the size of the range between min_num and max_num, which is 𝑂(𝑛).\n",
"3. Space for Iteration Variables:\n",
"\tfor i in range(min_num, max_num):\n",
"- The iteration variable i and the loop itself take 𝑂(1) space.\n",
"\n",
"Overall Space Complexity: The dominant term is the space used by miss_list, which results in 𝑂(𝑛) space complexity.\n",
"\n",
"Summary\n",
"Time Complexity: 𝑂(𝑛2) due to the nested loop checking each number's membership in the list.\n",
"Space Complexity: 𝑂(𝑛) primarily due to the miss_list that stores the missing numbers.\n",
"This detailed analysis helps us understand the performance characteristics of the function\n",
" and highlights areas where optimization might be needed, \n",
" such as improving the membership check to reduce the overall time complexity."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"- Critique your partner's solution, including explanation, and if there is anything that should be adjusted.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Your answer here\n",
"Critique of Partner's Solution\n",
"\n",
"The solution provided by my partner for the problem \n",
"of finding missing numbers in a list within the range [0, n] \n",
"has a clear logical flow but has several inefficiencies \n",
"and areas for improvement. Let's break down the critique step-by-step:\n",
"\n",
"Strengths:\n",
"Initial Check for Empty List: The code correctly checks if the input list is empty and returns -1, \n",
"which is a good practice to handle edge cases.\n",
"\n",
"if len(nums) == 0:\n",
" return -1\n",
"\t\n",
"Range Identification: The code correctly identifies the minimum and maximum values in the list, which can be helpful for certain types of range calculations.\n",
"\n",
"min_num = min(nums)\n",
"max_num = max(nums)\n",
"\n",
"Weaknesses and Areas for Improvement:\n",
"Incorrect Range Calculation:\n",
"\n",
"The problem specifies finding missing numbers in the range [0, n], \n",
"where n is typically the maximum value in the list. \n",
"The current solution calculates the range using both minimum and maximum values, \n",
"which is unnecessary and potentially incorrect.\n",
"Inefficient Membership Check:\n",
"\n",
"The use of if i not in nums inside the loop results in an 𝑂(𝑛2) time complexity. \n",
"This is because checking if an element is in a list is 𝑂(𝑛) \n",
"and it is done inside a loop that runs 𝑂(𝑛) times.\n",
"\n",
"for i in range(min_num, max_num):\n",
" if i not in nums:\n",
" miss_list.append(i)\n",
"\t\t\n",
"Unnecessary Complexity:\n",
"\n",
"The code includes some redundant steps and variables. \n",
"For instance, checking the length of miss_list twice and using both minimum and maximum values to create the range."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## Part 3:\n",
"\n",
"Please write a 200 word reflection documenting your process from assignment 1, and your presentation and review experience with your partner at the bottom of the Jupyter Notebook under a new heading \"Reflection.\" Again, export this Notebook as pdf.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Reflection"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Your answer here\n",
"In this assignment, my objective was to delve deeply into a data structures or algorithms question, \n",
"honing my skills to prepare for industry coding interviews. \n",
"I chose a problem from Leetcode, a widely-used platform for coding practice, to simulate the interview preparation process. \n",
"The problem involved identifying missing numbers from a given range in a list of integers. \n",
"Through this task, I aimed to refine my approach to understanding, solving, and optimizing coding problems.\n",
"\n",
"Process Overview:\n",
"\n",
"\t1. Problem Understanding: I started by thoroughly reading and interpreting the problem statement. \n",
"\tThis included understanding the inputs, outputs, and constraints.\n",
"\t2. Planning the Solution: I first conceptualized a brute-force approach to ensure clarity on the basic requirements. \n",
"\tThen, I moved towards an optimized solution using efficient data structures.\n",
"\t3. Implementation: I wrote the pseudocode and translated it into Python code. \n",
"\tThe initial solution involved iterating through the range and checking for membership, which was inefficient.\n",
"\t4. Optimization: I identified inefficiencies, particularly the use of the not in operator within a loop, leading to 𝑂(𝑛2) time complexity. By using sets, I improved the solution to 𝑂(𝑛) time complexity.\n",
"\t5. Testing: I tested the solution with various cases, including edge cases, to ensure robustness.\n",
"\t\n",
"Presentation and Review Experience:\n",
"\n",
"I presented my solution to my partner, explaining the rationale behind each step. \n",
"My partner provided valuable feedback, highlighting areas for improvement and suggesting more efficient approaches.\n",
"This collaborative review process was enlightening, as it reinforced the importance of peer feedback in refining coding skills.\n",
"Through this assignment, I not only enhanced my problem-solving abilities \n",
"but also learned the significance of clear communication and continuous improvement in coding interviews."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## Evaluation Criteria\n",
"\n",
"We are looking for the similar points as Assignment 1\n",
"\n",
"- Problem is accurately stated\n",
"\n",
"- New example is correct and easily understandable\n",
"\n",
"- Correctness, time, and space complexity of the coding solution\n",
"\n",
"- Clarity in explaining why the solution works, its time and space complexity\n",
"\n",
"- Quality of critique of your partner's assignment, if necessary\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Submission Information\n",
"\n",
"🚨 **Please review our [Assignment Submission Guide](https://github.com/UofT-DSI/onboarding/blob/main/onboarding_documents/submissions.md)** 🚨 for detailed instructions on how to format, branch, and submit your work. Following these guidelines is crucial for your submissions to be evaluated correctly.\n",
"\n",
"### Submission Parameters:\n",
"* Submission Due Date: `HH:MM AM/PM - DD/MM/YYYY`\n",
"* The branch name for your repo should be: `assignment-2`\n",
"* What to submit for this assignment:\n",
" * This Jupyter Notebook (assignment_2.ipynb) should be populated and should be the only change in your pull request.\n",
"* What the pull request link should look like for this assignment: `https://github.com/<your_github_username>/algorithms_and_data_structures/pull/<pr_id>`\n",
" * Open a private window in your browser. Copy and paste the link to your pull request into the address bar. Make sure you can see your pull request properly. This helps the technical facilitator and learning support staff review your submission easily.\n",
"\n",
"Checklist:\n",
"- [ ] Created a branch with the correct naming convention.\n",
"- [ ] Ensured that the repository is public.\n",
"- [ ] Reviewed the PR description guidelines and adhered to them.\n",
"- [ ] Verify that the link is accessible in a private browser window.\n",
"\n",
"If you encounter any difficulties or have questions, please don't hesitate to reach out to our team via our Slack at `#cohort-3-help`. Our Technical Facilitators and Learning Support staff are here to help you navigate any challenges.\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "scale2prod",
"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.12.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading
Loading