50 Interview Question Code Galatta - Handbook

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

50 Common Interview

Questions
Chapter 1 - Arrays
Question - 1
1. Two Sum
Question:
• Given an array of integers, find two numbers such that they add up to a specific target number.
• The function twoSum should return indices of the two numbers such that they add up to the target, where
index1 must be less than index2.
• Please note that your returned answers (both index1 and index2) are not zero-based.
• You may assume that each input would have exactly one solution.

Example 1:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2:
Input: nums = [3,2,4], target = 6
Output: [1,2]
Example 3:
Input: nums = [3,3], target = 6
Output: [0,1]
Difficulty: Easy, Frequency: High
1. Two Sum – Brute Force Solution
Solution:
Brute force - O(n2) runtime, O(1) space :
Loop through each element x and find if there is another value that equals to target – x. As finding another
value requires looping through the rest of array, its runtime complexity is O(n2).
public int[] twoSum(int[] nums, int target)
{
for (int i = 0; i < nums.length; i++)
{
for (int j = i + 1; j < nums.length; j++)
{
if (nums[i] + nums[j] == target)
{
return new int[] { i, j };
}
}
}
}
Animated Video
1. Two Sum – Brute Force Solution
Steps:
1. Create a user defined function names twoSum with the arguments as input array and target variable.
2. Using a for loop iterate through each element from index i= 0 to length of input array. (outler loop)
3. For each outer loop element we have to iterate through all the elements from j=i+1 to length of array
4. Using if condition statement check whether the sum of i th element(outer loop) and j th element(inner
loop) is equal to Target element
5. If true, then return the index i and j using return statement.

But the Time complexity of this algorithm is high O(n2)


1. Two Sum – Hash Map Solution
Solution: Hash Map O(n) runtime, O(n) space :

We could reduce the runtime complexity of looking up a value to O(1) using a hash map that maps a value to its index.
public int[] twoSum(int[] numbers, int target)
{ int complement;
HashMap<Integer,Integer> map = new HashMap< Integer,Integer >();
for (int i = 0; i < numbers.length; i++)
{
map.put(numbers[i],i);
}
for (int i = 0; i < numbers.length; i++)
{
complement=target-numbers[i];
if(map.containsKey(complement))
{
return new int[](i,map.get(complement);
}
else return new int[]{-1,-1};
}
}
1. Two Sum – Hash Map Solution
Target = 7

Input Array Original Input Index


1 2 4 5 15
i=0 i=1 i=2 i=3 i=4 KEY VALUE
1 0
Check Complement exist in Keys?
(Target value – Input Value) 2 1
7-1=6 4 2
Check Complement exist in Keys?
(Target value-Input Value) 5 3
7-2=5 15 4
Return (i, value of key 5)
i.e. indexes [1,3 ] are the output
1. Two Sum – Hash Map Solution
Steps:
1. Create a user defined function names twoSum with the arguments as input array and target variable.
2. Create a Hashmap variable map with <integer,interger>
3. Using a for loop iterate through each element from index i= 0 to length of input array.
4. Inside for loop using map.put() insert the key as the input array elements and value as index of the
elements ie.map.put(number[i],i);
5. Using another for loop iterate through the elements from index i=0 to length of array elements
6. Calculate the complement by subtracting the current index value from Target value ie..Complement
=target-numbers[i];Using containsKey
7. Using ContainsKey() check whether the complement value is present in the key of Hashmap if truw then
return the current index i and map.get(complement) which is the index value of the key.
return new int[](i,map.get(complement);
Question - 2
2. Two Sum II- Input array is sorted
Question:
• Given an array of sorted integers in ascending order, find two numbers such that they add up to a specific
target number.
• The function twoSum should return indices of the two numbers such that they add up to the target, where
index1 must be less than index2.
• Please note that your returned answers (both index1 and index2) are not zero-based.
• You may assume that each input would have exactly one solution.

Example 1:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2:
Input: nums = [2,3,4], target = 6
Output: [0,2]
Example 3:
Input: nums = [3,3], target = 6
Output: [0,1] Difficulty: Medium, Frequency: NA
2. Two Sum II- Input array is sorted
Solution 1:
• Of course we could still apply the [Hash table] approach, but it costs us O(n) extra space, plus it does not make
use of the fact that the input is already sorted.
O(n log n) runtime, O(1) space – Binary search:
• For each element x, we could look up if target – x exists in O(log n) time by applying binary search over the
sorted array. Total runtime complexity is O(n log n).

private int bsearch(int[] A, int key, int strt)


public int[] twoSum(int[] numbers, int target) {
{ int start = strt, end = A.length - 1;
// Assume input is already sorted. while (start < end) {
for (int i = 0; i < numbers.length; i++) int Mid = (start + end) / 2;
{ if (A[Mid] < key) {
int j = bsearch(numbers, target - numbers[i], i + 1 ); start = Mid + 1;
if (j != -1) { } else {
return new int[] { i + 1, j + 1 }; end = Mid;
} }
} }
} return (start == end && A[start] == key)?start:-1;
}
Animated Video
2. Two Sum II- Input array is sorted (Binary search)
Steps:
1. Create a user defined function names twoSum with the arguments as input array and target variable.
2. Using a for loop iterate through each element from index i= 0 to length of input array.
3. Inside for loop call the binarysearch function named bsearch() with arguments numbers[], key value
(Target-numbers[i]) and start value from i+1;
4. Inside bsearch() initialize start and end value from strt to length-1
5. Inside while loop with condition start<end, check calculate middle element using (start index+end
index)/2
6. If middle index element is < key then make the start value as middle+1
7. Else make end equal to middle. Repeat this steps until the start==end and start element value is equal to
key. Once it reached then return the matching elements index.
2. Two Sum II- Input array is sorted
Solution 2:
O(n) runtime, O(1) space – Two pointers:
• Let’s assume we have two indices pointing to the ith and jth elements, Ai and Aj respectively. The sum of Ai and
Aj could only fall into one of these three possibilities:
i. Ai + Aj > target. Increasing i isn’t going to help us, as it makes the sum even bigger. Therefore we should
decrement j.
ii. Ai + Aj < target. Decreasing j isn’t going to help us, as it makes the sum even smaller. Therefore we should
increment i.
iii. Ai + Aj == target. We have found the answer. else if (sum > target)
public int[] twoSum(int[] numbers, int target) {
{ end --;
int start = 0, end = numbers.length - 1; }
while (start < end) { else
int sum = numbers[start] + numbers[end]; {
if (sum < target) return new int[] {start + 1, end + 1 };
{ }
start ++; }
} }
2. Two Sum II- Input array is sorted (Two Pointers)
Animated Video

You might also like