50 Interview Question Code Galatta - Handbook
50 Interview Question Code Galatta - Handbook
50 Interview Question Code Galatta - Handbook
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.
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
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).