Flipkart Coding Questions and Answers
Sample Flipkart Coding Questions with Solution
Here, on Flipkart coding questions and answers page you will get sample DSA questions with solution which can be asked in Flipkart SDE 1 Coding Assessment.
Let you know that Flipkart mainly asks Data Structures and Algorithms based questions based Binary Search Tree, Graphs, Tries, Trees and Dynamic programming.
Checkout this page get all the Sample Flipkart Coding Questions with Solutions.
About Round 1 – Flipkart Coding Assessment
First stage of the Flipkart SDE-1 recruitment process is an online coding assessment, conducted on HackerRank.
This round serves as an initial screening to evaluate your problem solving and coding skills.
Duration: 90 minutes
Platform: HackerRank
Number of Questions: 3 coding problems
Difficulty Level: Medium to Hard
Topics Covered:
1. Binary Search Trees (BST)
2. Tries (Prefix Trees)
3. Tree and Graph Traversals (DFS, BFS)
4. Graph Algorithms (Cycle detection, shortest paths, connected components)
5. Dynamic Programming (Tabulation, memoization, optimization problems)
To succeed in this round, focus on writing clean, optimized code and thoroughly testing your solutions against edge cases.
After this round 2 Technical Interviews followed by Hiring Manager Round will be conducted for assessing the candidates.
You can also opt for C or C++ too.
Practice Coding Questions:
Checkout PrepInsta Prime:
Flipkart Coding Questions with Solutions
1. Program for Finding Minimum in a Rotated Sorted Array Problem
You are given a sorted array that has been rotated between 1 and n times. For example:
- If the array [1,2,3,4,5,6] is rotated 4 times, it becomes [3,4,5,6,1,2].
- If rotated 6 times, it goes back to [1,2,3,4,5,6].
Your task is to find the smallest element in this rotated array. The elements in the array are unique.
While finding the minimum in O(n) time is easy, you need to write an algorithm that works in O(logn) time.
Output: 0
Constraints:
- 1 <= nums.length <= 1000
- -1000 <= nums[i] <= 1000
Solution with Binary Search Method
Use binary search to narrow down the search space by checking if the middle element lies in the rotated part, giving an O(logn) solution.
- Time complexity: O(log 1)
- Space complexity: O(1)
Code
class Solution { public: int findMin(vector<int> &nums) { int res = nums[0]; int l = 0; int r = nums.size() - 1; while (l <= r) { if (nums[l] < nums[r]) { res = min(res, nums[l]); break; } int m = l + (r - l) / 2; res = min(res, nums[m]); if (nums[m] >= nums[l]) { l = m + 1; } else { r = m - 1; } } return res; } };
public class Solution { public int findMin(int[] nums) { int l = 0; int r = nums.length - 1; int res = nums[0]; while (l <= r) { if (nums[l] < nums[r]) { res = Math.min(res, nums[l]); break; } int m = l + (r - l) / 2; res = Math.min(res, nums[m]); if (nums[m] >= nums[l]) { l = m + 1; } else { r = m - 1; } } return res; } }
class Solution: def findMin(self, nums: List[int]) -> int: res = nums[0] l, r = 0, len(nums) - 1 while l <= r: if nums[l] < nums[r]: res = min(res, nums[l]) break m = (l + r) // 2 res = min(res, nums[m]) if nums[m] >= nums[l]: l = m + 1 else: r = m - 1 return res
class Solution { /** * @param {number[]} nums * @return {number} */ findMin(nums) { let l = 0; let r = nums.length - 1; let res = nums[0]; while (l <= r) { if (nums[l] <= nums[r]) { res = Math.min(res, nums[l]); break; } let m = l + Math.floor((r - l) / 2); res = Math.min(res, nums[m]); if (nums[m] >= nums[l]) { l = m + 1; } else { r = m - 1; } } return res; } }
2. Design Add and Search Words Data Structure
Design a data structure to store words and efficiently support word addition and searching, including flexible matching.
Implement the WordDictionary class with the following methods:
- addWord(word): Adds the given word to the data structure, allowing it to be stored for future searches. This is useful for building a collection of words dynamically.
- search(word): Searches for a word in the data structure. The search supports both exact matches and partial matches using the . character as a wildcard. The . can represent any letter, making the search versatile for patterns or incomplete words.
This structure is particularly useful for applications like pattern matching or word suggestions.
["WordDictionary", "addWord", "day", "addWord", "bay", "addWord", "may", "search", "say", "search", "day", "search", ".ay", "search", "b.."]
Output:
[null, null, null, null, false, true, true, true]
Explanation:
- WordDictionary wordDictionary = new WordDictionary();
- wordDictionary.addWord(“day”);
- wordDictionary.addWord(“bay”);
- wordDictionary.addWord(“may”);
- wordDictionary.search(“say”); // return false
- wordDictionary.search(“day”); // return true
- wordDictionary.search(“.ay”); // return true
- wordDictionary.search(“b..”); // return true
Constraints:
- 1 <= word.length <= 20
- word in addWord consists of lowercase English letters.
- word in search consist of ‘.’ or lowercase English letters.
Solving with Brute Force Method
Words are stored in a list, and each search checks all words by iterating through them, including handling wildcards (.) character by character. This leads to a time complexity of O(m * n), where m is the number of words and n is the word length.
- Time complexity: O(n) for addWord(), O(m∗n) for search().
- Space complexity: O(m*n)
where m is the number of words added and n is the length of the string.
Code
class WordDictionary { public: vector<string> store; WordDictionary() {} void addWord(string word) { store.push_back(word); } bool search(string word) { for (string w : store) { if (w.length() != word.length()) continue; int i = 0; while (i < w.length()) { if (w[i] == word[i] || word[i] == '.') { i++; } else { break; } } if (i == w.length()) { return true; } } return false; } };
public class WordDictionary { private List<String> store; public WordDictionary() { store = new ArrayList<>(); } public void addWord(String word) { store.add(word); } public boolean search(String word) { for (String w : store) { if (w.length() != word.length()) continue; int i = 0; while (i < w.length()) { if (w.charAt(i) == word.charAt(i) || word.charAt(i) == '.') { i++; } else { break; } } if (i == w.length()) { return true; } } return false; } }
class WordDictionary: def __init__(self): self.store = [] def addWord(self, word: str) -> None: self.store.append(word) def search(self, word: str) -> bool: for w in self.store: if len(w) != len(word): continue i = 0 while i < len(w): if w[i] == word[i] or word[i] == '.': i += 1 else: break if i == len(w): return True return False
class WordDictionary { constructor() { this.store = []; } /** * @param {string} word * @return {void} */ addWord(word) { this.store.push(word); } /** * @param {string} word * @return {boolean} */ search(word) { for (let w of this.store) { if (w.length !== word.length) continue; let i = 0; while (i < w.length) { if (w[i] === word[i] || word[i] === '.') { i++; } else { break; } } if (i === w.length) { return true; } } return false; } }
Flipkart Coding Questions with Solutions
3. Maximum Depth of Binary Tree
You are given the root of a binary tree, and your task is to determine its depth.
Depth of a binary tree refers to the number of nodes present along the longest path starting from the root node down to the farthest leaf node.
In simpler terms, it measures how deep the tree goes from the topmost node (the root) to the bottommost node (a leaf with no children). This depth represents the maximum number of steps needed to traverse the tree vertically.
Example:

Output: 3
Constraints:
- 0 <= The number of nodes in the tree <= 100.
- -100 <= Node.val <= 100
Solving with Iterative DFS(Stack) Method
This method uses a stack to simulate the depth-first traversal iteratively. Each node is paired with its depth and pushed onto the stack. As you process each node, you update the maximum depth by comparing the current depth of the node with the recorded value.
- Time complexity: O(n)
- Space complexity: O(n)
Code
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: int maxDepth(TreeNode* root) { stack> stack; stack.push({root, 1}); int res = 0; while (!stack.empty()) { pair current = stack.top(); stack.pop(); TreeNode* node = current.first; int depth = current.second; if (node != nullptr) { res = max(res, depth); stack.push({node->left, depth + 1}); stack.push({node->right, depth + 1}); } } return res; } };
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ public class Solution { public int maxDepth(TreeNode root) { Stack> stack = new Stack<>(); stack.push(new Pair<>(root, 1)); int res = 0; while (!stack.isEmpty()) { Pair current = stack.pop(); TreeNode node = current.getKey(); int depth = current.getValue(); if (node != null) { res = Math.max(res, depth); stack.push(new Pair<>(node.left, depth + 1)); stack.push(new Pair<>(node.right, depth + 1)); } } return res; } }
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def maxDepth(self, root: Optional[TreeNode]) -> int: stack = [[root, 1]] res = 0 while stack: node, depth = stack.pop() if node: res = max(res, depth) stack.append([node.left, depth + 1]) stack.append([node.right, depth + 1]) return res
/** * Definition for a binary tree node. * class TreeNode { * constructor(val = 0, left = null, right = null) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { /** * @param {TreeNode} root * @return {number} */ maxDepth(root) { const stack = [[root, 1]]; let res = 0; while (stack.length > 0) { const current = stack.pop(); const node = current[0]; const depth = current[1]; if (node !== null) { res = Math.max(res, depth); stack.push([node.left, depth + 1]); stack.push([node.right, depth + 1]); } } return res; } }
4. Number of Connected Components In An Undirected Graph Problem
Given an undirected graph with n nodes, an edges array is provided where edges[i] = [a, b] indicates an edge between node a and node b.
The nodes are numbered from 0 to n – 1. The task is to return the total number of connected components in the graph.
edges=[[0,1], [0,2]]
Output: 1
Constraints:
- 1 <= n <= 100
- 0 <= edges.length <= n * (n – 1) / 2
Solving with Disjoint Set Union (Rank | Size) method
DSU tracks the connected components using a union-find approach, where nodes are grouped into sets. Using union by rank or size ensures efficient merging of components, and the number of connected components is determined by the number of distinct sets.
- Time complexity: O(V+(E∗α(V)))
- Space complexity: O(V)
Where V is the number of vertices and E is the number of edges.
Code:
class DSU { public: vectorparent; vector rank; DSU(int n) { parent.resize(n); rank.resize(n, 1); for (int i = 0; i < n; i++) { parent[i] = i; } } int find(int node) { int cur = node; while (cur != parent[cur]) { parent[cur] = parent[parent[cur]]; cur = parent[cur]; } return cur; } bool unionSets(int u, int v) { int pu = find(u); int pv = find(v); if (pu == pv) { return false; } if (rank[pv] > rank[pu]) { swap(pu, pv); } parent[pv] = pu; rank[pu] += rank[pv]; return true; } }; class Solution { public: int countComponents(int n, vector >& edges) { DSU dsu(n); int res = n; for (auto& edge : edges) { if (dsu.unionSets(edge[0], edge[1])) { res--; } } return res; } };
public class DSU { int[] parent; int[] rank; public DSU(int n) { parent = new int[n]; rank = new int[n]; for (int i = 0; i < n; i++) { parent[i] = i; rank[i] = 1; } } public int find(int node) { int cur = node; while (cur != parent[cur]) { parent[cur] = parent[parent[cur]]; cur = parent[cur]; } return cur; } public boolean union(int u, int v) { int pu = find(u); int pv = find(v); if (pu == pv) { return false; } if (rank[pv] > rank[pu]) { int temp = pu; pu = pv; pv = temp; } parent[pv] = pu; rank[pu] += rank[pv]; return true; } } public class Solution { public int countComponents(int n, int[][] edges) { DSU dsu = new DSU(n); int res = n; for (int[] edge : edges) { if (dsu.union(edge[0], edge[1])) { res--; } } return res; } }
class DSU: def __init__(self, n): self.parent = list(range(n)) self.rank = [1] * n def find(self, node): cur = node while cur != self.parent[cur]: self.parent[cur] = self.parent[self.parent[cur]] cur = self.parent[cur] return cur def union(self, u, v): pu = self.find(u) pv = self.find(v) if pu == pv: return False if self.rank[pv] > self.rank[pu]: pu, pv = pv, pu self.parent[pv] = pu self.rank[pu] += self.rank[pv] return True class Solution: def countComponents(self, n: int, edges: List[List[int]]) -> int: dsu = DSU(n) res = n for u, v in edges: if dsu.union(u, v): res -= 1 return res
class DSU { /** * @param {number} n */ constructor(n) { this.parent = Array.from({ length: n }, (_, i) => i); this.rank = Array(n).fill(1); } /** * @param {number} node * @return {number} */ find(node) { let cur = node; while (cur !== this.parent[cur]) { this.parent[cur] = this.parent[this.parent[cur]]; cur = this.parent[cur]; } return cur; } /** * @param {number} u * @param {number} v * @return {boolean} */ union(u, v) { let pu = this.find(u); let pv = this.find(v); if (pu === pv) { return false; } if (this.rank[pv] > this.rank[pu]) { [pu, pv] = [pv, pu]; } this.parent[pv] = pu; this.rank[pu] += this.rank[pv]; return true; } } class Solution { /** * @param {number} n * @param {number[][]} edges * @returns {number} */ countComponents(n, edges) { const dsu = new DSU(n); let res = n; for (const [u, v] of edges) { if (dsu.union(u, v)) { res--; } } return res; } }
Flipkart Coding Questions
5. Maximum Product Subarray Problem
Given an integer array nums, find the subarray with the maximum product and return its value.
A sub array is a continuous, non-empty portion of the array.
You can assume the result will fit within a 32-bit integer.
Output: 4
Constraints:
- 1 <= nums.length <= 1000
- -10 <= nums[i] <= 10
Solving with Kadane’s Algorithm method
Modify Kadane’s algorithm to handle products by keeping track of both the maximum and minimum products at each index. This efficiently handles both positive and negative numbers.
- Time complexity: O(n)
- Space complexity: O(1)
Code
class Solution { public: int maxProduct(vector<int>& nums) { int res = nums[0]; int curMin = 1, curMax = 1; for (int num : nums) { int tmp = curMax * num; curMax = max(max(num * curMax, num * curMin), num); curMin = min(min(tmp, num * curMin), num); res = max(res, curMax); } return res; } };
public class Solution { public int maxProduct(int[] nums) { int res = nums[0]; int curMin = 1, curMax = 1; for (int num : nums) { int tmp = curMax * num; curMax = Math.max(Math.max(num * curMax, num * curMin), num); curMin = Math.min(Math.min(tmp, num * curMin), num); res = Math.max(res, curMax); } return res; } }
class Solution: def maxProduct(self, nums: List[int]) -> int: res = nums[0] curMin, curMax = 1, 1 for num in nums: tmp = curMax * num curMax = max(num * curMax, num * curMin, num) curMin = min(tmp, num * curMin, num) res = max(res, curMax) return res
class Solution { /** * @param {number[]} nums * @return {number} */ maxProduct(nums) { let res = nums[0]; let curMin = 1; let curMax = 1; for (const num of nums) { const tmp = curMax * num; curMax = Math.max(Math.max(num * curMax, num * curMin), num); curMin = Math.min(Math.min(tmp, num * curMin), num); res = Math.max(res, curMax); } return res; } }
6. Merging Triplets to Form a Target Triplet
The Merge Triplets to Form Target problem involves determining whether you can construct a specific triplet using a series of merging operations on given triplets.
Problem Description
Input
- triplets: A 2D list where each triplet is of the form [a, b, c].
- target: A triplet [x, y, z] that we aim to construct.
Operation
For two triplets triplets[i] = [ai, bi, ci] and triplets[j] = [aj, bj, cj], you can update triplets[j] as follows:
triplets[j]=[max(ai,aj),max(bi,bj),max(ci,cj)]
Goal
Determine whether it is possible to make target an element of triplets after performing the allowed operations.
Constraints:
- 1 <= triplets.length <= 1000
- 1 <= ai, bi, ci, x, y, z <= 100
There are mainly two approach to solve this problem –
- Greedy
- Greedy (Optimal)
Flipkart Coding Questions and Answers
1. Greedy
- Time complexity: O(n)
- Space complexity: O(1)
class Solution: def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool: good = set() for t in triplets: if t[0] > target[0] or t[1] > target[1] or t[2] > target[2]: continue for i, v in enumerate(t): if v == target[i]: good.add(i) return len(good) == 3
class Solution { public: bool isNStraightHand(vector& hand, int groupSize) { if (hand.size() % groupSize != 0) return false; unordered_map count; for (int num : hand) count[num]++; sort(hand.begin(), hand.end()); for (int num : hand) { if (count[num] > 0) { for (int i = num; i < num + groupSize; i++) { if (count[i] == 0) return false; count[i]--; } } } return true; } };
public class Solution { public boolean mergeTriplets(int[][] triplets, int[] target) { Setgood = new HashSet<>(); for (int[] t : triplets) { if (t[0] > target[0] || t[1] > target[1] || t[2] > target[2]) { continue; } for (int i = 0; i < t.length; i++) { if (t[i] == target[i]) { good.add(i); } } } return good.size() == 3; } }
class Solution { /** * @param {number[][]} triplets * @param {number[]} target * @return {boolean} */ mergeTriplets(triplets, target) { const good = new Set(); for (const t of triplets) { if (t[0] > target[0] || t[1] > target[1] || t[2] > target[2]) { continue; } for (let i = 0; i < t.length; i++) { if (t[i] === target[i]) { good.add(i); } } } return good.size === 3; } }
2. Greedy (Optimal)
Time & Space Complexity
- Time complexity: O(n)
- Space complexity: O(1)
class Solution { public: bool mergeTriplets(vector>& triplets, vector & target) { bool x = false, y = false, z = false; for (const auto& t : triplets) { x |= (t[0] == target[0] && t[1] <= target[1] && t[2] <= target[2]); y |= (t[0] <= target[0] && t[1] == target[1] && t[2] <= target[2]); z |= (t[0] <= target[0] && t[1] <= target[1] && t[2] == target[2]); if (x && y && z) return true; } return false; } };
public class Solution { public boolean mergeTriplets(int[][] triplets, int[] target) { boolean x = false, y = false, z = false; for (int[] t : triplets) { x |= (t[0] == target[0] && t[1] <= target[1] && t[2] <= target[2]); y |= (t[0] <= target[0] && t[1] == target[1] && t[2] <= target[2]); z |= (t[0] <= target[0] && t[1] <= target[1] && t[2] == target[2]); if (x && y && z) { return true; } } return false; } }
class Solution: def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool: x = y = z = False for t in triplets: x |= (t[0] == target[0] and t[1] <= target[1] and t[2] <= target[2]) y |= (t[0] <= target[0] and t[1] == target[1] and t[2] <= target[2]) z |= (t[0] <= target[0] and t[1] <= target[1] and t[2] == target[2]) if x and y and z: return True return False
class Solution { /** * @param {number[][]} triplets * @param {number[]} target * @return {boolean} */ mergeTriplets(triplets, target) { let x = false, y = false, z = false; for (let t of triplets) { x |= (t[0] === target[0] && t[1] <= target[1] && t[2] <= target[2]); y |= (t[0] <= target[0] && t[1] === target[1] && t[2] <= target[2]); z |= (t[0] <= target[0] && t[1] <= target[1] && t[2] === target[2]); if (x && y && z) return true; } return false; } }
If you want to practice more for Flipkart Coding Assessment, you can visit our Coding Blogs that will surely help you to prepare for Flipkart SDE 1 Coding Assessment and Flipkart Technical Interviews.
Practice Coding Questions:
Checkout PrepInsta Prime:
Faq's Related to Flipkart Coding Assessment
Answer:
The coding questions typically range from medium to hard difficulty, covering data structures, algorithms, and problem-solving logic relevant to real-world scenarios.
Answer:
Yes, Flipkart often includes problems based on Trees, Graphs, Binary Search Trees (BST), and Tries, so it’s important to have a solid grasp of these topics.
Answer:
Yes, HackerRank allows you to code in your preferred programming language, though proficiency in C++, Java, or Python is recommended due to better support and familiarity.