Java Python Arrays
Category: Arrays & Hash Maps
This guide covers five popular interview questions in both Java and Python.
Two Sum
Overview
Two Sum is one of the most frequently asked coding interview questions.
Algorithm
Hash map lookup.
Complexity: O(n)
Tip: Use a dictionary/hash map to store visited values.
Python
class Solution:
def solve(self,*args):
# TODO: implement Two Sum
pass
Java
class Solution {
// TODO: implement Two Sum
}
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Contains Duplicate
Overview
Contains Duplicate is one of the most frequently asked coding interview questions.
Algorithm
Track seen values.
Complexity: O(n)
Tip: A set gives constant-time lookups.
Python
class Solution:
def solve(self,*args):
# TODO: implement Contains Duplicate
pass
Java
class Solution {
// TODO: implement Contains Duplicate
}
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Best Time to Buy Stock
Overview
Best Time to Buy Stock is one of the most frequently asked coding interview questions.
Algorithm
Track minimum.
Complexity: O(n)
Tip: Keep the lowest price seen so far.
Python
class Solution:
def solve(self,*args):
# TODO: implement Best Time to Buy Stock
pass
Java
class Solution {
// TODO: implement Best Time to Buy Stock
}
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Product Except Self
Overview
Product Except Self is one of the most frequently asked coding interview questions.
Algorithm
Prefix/suffix.
Complexity: O(n)
Tip: Avoid division.
Python
class Solution:
def solve(self,*args):
# TODO: implement Product Except Self
pass
Java
class Solution {
// TODO: implement Product Except Self
}
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Top K Frequent
Overview
Top K Frequent is one of the most frequently asked coding interview questions.
Algorithm
Frequency map.
Complexity: O(n log k)
Tip: Use a heap.
Python
class Solution:
def solve(self,*args):
# TODO: implement Top K Frequent
pass
Java
class Solution {
// TODO: implement Top K Frequent
}
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.
Interview advice: explain your approach before coding, discuss edge cases, analyse time and
space complexity, and test your solution using small examples. Practising this problem
repeatedly improves pattern recognition.