Today we will be looking at one of the interviewers’ favorite question for the technical coding interview. Two Sum. Given an array of integers nums and an integer target , return indices of the two numbers such that they add up to target . You may ...
Today we will be looking at one of the interviewers’ favorite question for the technical coding interview.
1. Two Sum
Given an array of integers
nums and an integer
target, return _indices of the two numbers such that they add up to `target_`.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
Input: nums = [2,7,11,15], target = 9 Output: [0,1] Output: Because nums + nums == 9, we return [0, 1].
Input: nums = [3,2,4], target = 6 Output: [1,2]
Input: nums = [3,3], target = 6 Output: [0,1]
2 <= nums.length <= 105
-109 <= nums[i] <= 109
-109 <= target <= 109
One of the first solutions that came to my mind after reading the question is going through each element and check if the addition of any 2 elements results in the answer. If yes, we return the index of both the elements else we continue the process until we find the answer.
The code would look like the following.
class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: for a in range(len(nums)): for b in range(a+1, len(nums)): if nums[a]+nums[b] == target: return [a,b]
Considering the worst-case scenario, wherein our output is at the end of the list. In that case, we would take up O(N*(N-1)) time which is O(N²).
We are not using any extra space and hence the space complexity will be O(1).
In this tutorial, you’re going to learn a variety of Python tricks that you can use to write your Python code in a more readable and efficient way like a pro.
Today you're going to learn how to use Python programming in a way that can ultimately save a lot of space on your drive by removing all the duplicates. We gonna use Python OS remove( ) method to remove the duplicates on our drive. Well, that's simple you just call remove ( ) with a parameter of the name of the file you wanna remove done.
In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.
Magic Methods are the special methods which gives us the ability to access built in syntactical features such as ‘<’, ‘>’, ‘==’, ‘+’ etc.. You must have worked with such methods without knowing them to be as magic methods. Magic methods can be identified with their names which start with __ and ends with __ like __init__, __call__, __str__ etc. These methods are also called Dunder Methods, because of their name starting and ending with Double Underscore (Dunder).
Having to handle exceptions is common in Python and so is having to define your own. Yet, I have seen competing ways of doing so in various projects. The inconsistency comes from Exceptions being something that can easily be subclassed and extended, but also something that can be easily instantiated and used in their base form.