Top Coding
Interview Questions
(0-2 years)
25 - 35 LPA
Swipe>>
@codebuilderhq
Follow to Get this
1️⃣ Group Anagrams
📌 Problem Statement:
Given an array of strings, group the strings that are
anagrams of each other.
🔹 Input: ["eat","tea","tan","ate","nat","bat"]
🔹 Output: [ ["eat","tea","ate"], ["tan","nat"], ["bat"] ]
💻 Java Code:
import [Link].*;
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> map = new HashMap<>();
for(String s : strs){
char[] arr = [Link]();
[Link](arr);
String key = new String(arr);
[Link](key, k -> new ArrayList<>
()).add(s);
}
return new ArrayList<>([Link]());
}
}
⏱ Time Complexity: O(N * K log K)
📦 Space Complexity: O(N * K)
2️⃣ Merge K Sorted Lists
📌 Problem Statement:
Given an array of k sorted linked lists, merge them and
return the single sorted linked list.
🔹 Input: ["eat","tea","tan","ate","nat","bat"]
🔹 Output: [ ["eat","tea","ate"], ["tan","nat"], ["bat"] ]
💻 Java Code:
import [Link].*;
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
PriorityQueue<ListNode> pq = new PriorityQueue<>((a, b) -> [Link] - [Link]);
for(ListNode node : lists){
if(node != null) [Link](node);
}
ListNode dummy = new ListNode(0);
ListNode current = dummy;
⏱ Time Complexity: O(N log K)
while(![Link]()){
ListNode min = [Link]();
[Link] = min;
📦 Space Complexity: O(K)
current = [Link];
if([Link] != null) [Link]([Link]);
}
return [Link];
}
}
3️⃣ Find Median from a Data Stream
📌 Problem Statement:
Design a data structure that supports adding numbers from
a stream and retrieving the median efficiently at any time.
You must implement:
addNum(int num) →adds a number to the stream
findMedian() →
returns the current median of all
elements added so far
🔹 Input:
addNum(1)
addNum(2)
findMedian() → 1.5addNum(3)
findMedian() →2
🔹 Output:
1.5
2
💻 Java Code:
import [Link].*;
class MedianFinder {
PriorityQueue<Integer> small = new PriorityQueue<>
([Link]());
PriorityQueue<Integer> large = new PriorityQueue<>();
public void addNum(int num) {
[Link](num);
[Link]([Link]());
if([Link]() > [Link]()){
[Link]([Link]());
}
}
public double findMedian() {
if([Link]() > [Link]()) return [Link]();
return ([Link]() + [Link]()) / 2.0;
}
}
⏱ Time Complexity: O(N * K log K)
📦 Space Complexity: O(N * K)
4️⃣ Monotonic Array
📌 Problem Statement:
An array is considered monotonic if it is either entirely non-
increasing or entirely non-decreasing.
🔹 Input: [1, 2, 2, 3]
🔹 Output: true // non-decreasing
💻 Java Code:
class Solution {
public boolean isMonotonic(int[] nums) {
boolean inc = true, dec = true;
for(int i=1;i<[Link];i++){
if(nums[i] > nums[i-1]) dec = false;
if(nums[i] < nums[i-1]) inc = false;
}
return inc || dec;
}
}
⏱ Time Complexity: O(N)
📦 Space Complexity: O(1)
5️⃣ LRU Cache
📌 Problem Statement:
Design a data structure that follows the LRU (Least
Recently Used) eviction policy.
Implement an LRU Cache with:
get(int key)→ return value if key exists, otherwise
return -1
→
put(int key, int value) insert/update a value
If the cache reaches capacity, remove the least
recently used entry.
🔹 Input:
LRUCache cache = new LRUCache(2); 🔹 Output:
[Link](1, 1); 1
[Link](2, 2); -1
[Link](1); // returns 1
-1
[Link](3, 3); // evicts key 2
[Link](2); // returns -1
3
[Link](4, 4); // evicts key 3 4
[Link](1); // returns -1
[Link](3); // returns 3
[Link](4); // returns 4
💻 Java Code Implementation (Using LinkedHashMap)
import [Link].*;
class LRUCache {
private final int capacity;
private final LinkedHashMap<Integer, Integer> cache;
public LRUCache(int capacity) {
[Link] = capacity;
[Link] = new LinkedHashMap<>(capacity, 0.75f, true);
}
public int get(int key) {
return [Link](key, -1);
}
public void put(int key, int value) {
if([Link](key)){
[Link](key, value);
return; ⏱ →
Time Complexity: put O(1) , get → 0(1)
} 📦 Space Complexity: O(capacity)
if([Link]() == capacity){
int oldestKey = [Link]().iterator().next();
[Link](oldestKey);
}
[Link](key, value);
}
}
6️⃣ LFU Cache
📌 Problem Statement:
Design a data structure that follows the LFU — Least
Frequently Used cache eviction policy.
Your cache should support:
get(int key) → return the value if present, else -1
put(int key, int value) →
insert/update the key-value
pair
📌 Rules:
When the cache reaches capacity, remove the least
frequently used item.
If two items share the same frequency, remove the one
that was used least recently.
Both operations should run in O(1) time average.
🔹 Output:
🔹 Input: 1
LFUCache cache = new LFUCache(2);
-1
[Link](1, 1);
3
[Link](2, 2);
[Link](1); // returns 1 (freq of key 1 → 2) -1
[Link](3, 3);
💻 Java Code Implementation
(HashMap + Frequency Buckets)
import [Link].*;
class LFUCache {
class Node {
int key, value, freq;
Node(int k, int v) { key = k; value = v; freq = 1; }
}
private final int capacity;
private int minFreq = 0;
private final Map<Integer, Node> cache = new
HashMap<>();
private final Map<Integer, LinkedHashSet<Node>>
freqMap = new HashMap<>();
public LFUCache(int capacity) {
[Link] = capacity;
}
public int get(int key) {
if () return -1;
updateFrequency([Link](key));
return [Link](key).value;
}
public void put(int key, int value) {
⏱ Time & Space Complexity
if (capacity == 0) return;
Time Space
Operation
Complexity Complexity
if ([Link](key)) {
Node node = [Link](key); get() O(1) O(capacity)
[Link] = value;
updateFrequency(node);
put() O(1) O(capacity)
return;
}
if ([Link]() == capacity) {
Node old = [Link](minFreq).iterator().next();
[Link](minFreq).remove(old);
[Link]([Link]);
}
Node newNode = new Node(key, value);
[Link](key, newNode);
[Link](1, k -> new LinkedHashSet<>()).add(newNode);
minFreq = 1;
}
private void updateFrequency(Node node) {
[Link]([Link]).remove(node);
if ([Link] == minFreq && [Link]([Link]).isEmpty()) {
minFreq++;
}
[Link]++;
[Link]([Link], k -> new LinkedHashSet<>()).add(node);
}
}
7️⃣ Min Stack
📌 Problem Statement:
Design a stack that supports the following operations in
O(1) time:
push(int val) — Add an element to the stack
pop() — Remove the top element
top() — Get the top element
getMin() — Retrieve the minimum element in the stack
at any time
🔹 Input:
MinStack stack = new MinStack(); 🔹 Output:
[Link](5); 3
[Link](3); 3
[Link](7);
5
[Link](); // returns 3
[Link]();
[Link](); // returns 3
[Link]();
[Link](); // returns 5
💻 Java Code Implementation
import [Link].*;
class MinStack {
private Stack<Integer> stack;
private Stack<Integer> minStack;
public MinStack() {
stack = new Stack<>();
minStack = new Stack<>();
}
public void push(int val) {
[Link](val);
if([Link]() || val <= [Link]()) {
[Link](val);
}
}
public void pop() {
if([Link]().equals([Link]())) {
[Link]();
}
}
public int top() {
return [Link]();
}
public int getMin() {
return [Link]();
}
}
⏱ Time & Space Complexity
Operation Time Complexity Space Complexity
push() O(1) O(N)
pop() O(1) O(N)
top() O(1) O(N)
getMin() O(1) O(N)
8️⃣ Validate Stack Sequences
📌 Problem Statement:
You are given two integer arrays pushed and popped, each
containing unique values.
Both arrays represent a sequence of stack operations:
pushed[i] means the value is pushed into the stack.
popped[i] means the value should be popped from the
stack.
Return true if the popped sequence can be obtained
from the pushed sequence using a stack, otherwise
return false.
Input:
pushed = [1, 2, 3, 4, 5]
popped = [4, 5, 3, 2, 1]
Output: true
💻 Java Code:
import [Link].*;
class Solution {
public boolean validateStackSequences(int[] pushed, int[]
popped) {
Stack<Integer> stack = new Stack<>();
int j = 0;
for (int num : pushed) {
[Link](num);
while (![Link]() && [Link]() == popped[j]) {
[Link]();
j++;
}
}
return [Link]();
}
⏱ Time Complexity: O(N)
}
📦 Space Complexity: O(N)
“Want the Complete PDF of All NetFlix
Interview Coding Questions?”
Comment “NetFlix”
👥 Don’t forget to Follow @codebuilderhq
for daily coding interview content 🚀