Q. Find the diameter of a binary tree.
asked 17xmediumTreesOnline test, Technical2012-2022
Ans. Use a postorder DFS that returns the height of each subtree and updates a global maximum diameter at every node. For each node, the longest path through it is left height plus right height, measured in edges. Visit each node once, so the time complexity is O(n), with O(h) recursion stack space.
Q. Check whether a given binary tree is a Binary Search Tree (BST).
asked 17xmediumTreesManagerial, Online test, Technical2012-2019
Ans. Check it by traversing the tree recursively with an allowed value range for each node. The root can have an infinite range; the left child must be less than the node, and the right child greater. Use the call stack as the data structure. Time complexity is O(n), space is O(h).
Q. Search an element in a row-wise and column-wise sorted matrix
asked 17xeasyArraysOnline test, Technical2014-2021
Ans. Start from the top-right element and eliminate one row or one column at a time. If the current value equals the target, return found. If it is greater, move left. If it is smaller, move down. This works because rows and columns are sorted. Time complexity is O(m + n), space is O(1).
Q. Design and implement an LRU Cache.
asked 16xmediumDesignManagerial, Technical2016-2024
Ans. Implement an LRU cache with a hash map from key to list node and a doubly linked list ordered by recent use. On get, return the value and move the node to the front. On put, update or insert at the front. If capacity is exceeded, remove the tail. Both operations are O(1).
Q. Find the next greater element for each element in an array.
asked 16xmediumArraysManagerial, Online test, Technical2014-2022
Ans. Use a monotonic decreasing stack to find the next greater element for each array value in O(n) time. Traverse from right to left, popping values less than or equal to the current element. The stack top is then the next greater element, or -1 if the stack is empty. Push the current element afterwards.
Q. What is the difference between a process and a thread?
asked 16xeasyOperating systemsManagerial, Technical2012-2021
Ans. A process is an independent running program with its own memory space, while a thread is a smaller unit of execution within a process that shares that process’s memory. Processes are more isolated and cost more to create or switch between. Threads are lighter, but shared memory makes synchronisation and race conditions important.
Q. Print the top view of a binary tree.
asked 15xmediumTreesOnline test, Technical2015-2022
Ans. Use level order traversal with a horizontal distance for each node, taking the first node seen at every distance. Store nodes in a queue with their distance, put the first value for each distance in an ordered map, then print map values from left to right. Time is O(n log n), or O(n) with hashing plus min and max distance.
Q. Print the left view of a binary tree.
asked 14xeasyTreesOnline test, Technical2013-2024
Ans. Print the first node visible at each depth when the tree is viewed from the left. Do a level order traversal using a queue, and for each level print the first node removed from the queue. This visits every node once, so the time complexity is O(n), with O(w) space for the queue.
Q. Add two numbers represented by linked lists.
asked 12xmediumLinked listsManagerial, Online test, Technical2014-2021
Ans. Use a dummy head and add corresponding digits while carrying overflow, creating one result node per digit. Traverse both lists together, treating missing digits as zero, and continue while either list has nodes or carry remains. This handles different lengths and final carry. Time is O(max(m, n)); extra space is the output list.
Q. Find the Lowest Common Ancestor (LCA) of two nodes in a Binary Tree.
asked 12xmediumTreesOnline test, Technical2013-2022
Ans. Use a recursive depth first search: if the current root is null or equals either target node, return it. Search left and right subtrees. If both return non-null, the current root is the LCA; otherwise return the non-null side. This uses the call stack, with linear time and tree-height space.
Q. Count the number of inversions in an array
asked 11xmediumArraysOnline test, Technical2014-2021
Ans. Use a modified merge sort to count inversions in O(n log n) time. While merging two sorted halves, if an element from the right half is smaller than one from the left, it forms inversions with all remaining elements in the left half. Add that count, merge normally, and return the total.
Q. Print the boundary traversal of a binary tree.
asked 11xmediumTreesOnline test, Technical2014-2023
Ans. Print the root, then the left boundary excluding leaves, then all leaves left to right, then the right boundary excluding leaves in reverse. Use recursive or iterative tree traversal, storing the right boundary in a stack or list before reversing. This avoids duplicates. Time complexity is O(n), with O(h) recursion space.
Q. Find the first non-repeating character in a given string.
asked 11xeasyStringsOnline test, Technical2014-2023
Ans. Scan the string to count each character, then scan it again and return the first character whose count is one. Use a hash map or fixed-size frequency array, depending on the character set. This keeps the order check simple and runs in O(n) time with O(k) space, where k is the number of distinct characters.
Q. Reverse a linked list in groups of size k.
asked 10xmediumLinked listsManagerial, Online test, Technical2013-2023
Ans. Reverse each block of k nodes by rewiring next pointers, then connect the previous block’s tail to the new head of the reversed block. Use three pointers to reverse a block in place, and first check that k nodes remain if partial groups should stay unchanged. Time complexity is O(n), space complexity is O(1).
Q. Clone a linked list with next and random pointer
asked 10xmediumLinked listsOnline test, Technical2012-2020
Ans. Clone it by first creating a copy node for every original node, storing original to copy in a hash map, then set each copy’s next and random using that map. The key detail is to create all nodes before wiring random pointers. This takes O(n) time and O(n) extra space.
Q. Explain ACID properties in DBMS
asked 10xeasyDBMSTechnical2015-2021
Ans. ACID properties are the guarantees that make database transactions reliable: Atomicity, Consistency, Isolation and Durability. Atomicity means all or nothing, Consistency keeps valid rules, Isolation prevents concurrent transactions interfering, and Durability ensures committed changes survive crashes. They are essential for correctness in systems handling critical data.
Q. Find the vertical sum in a binary tree
asked 9xmediumTreesOnline test, Technical2013-2020
Ans. Use a traversal while tracking each node’s horizontal distance from the root, then add the node value to a map keyed by that distance. The root has distance 0, the left child is distance minus 1, and the right child is distance plus 1. This takes O(n) time and O(n) space.
Q. Reverse every k nodes in a linked list.
asked 9xmediumLinked listsSystem design, Technical2012-2019
Ans. Reverse the linked list in groups of k by first checking that k nodes exist, then reversing only that block. Use a dummy head and three pointers: previous group tail, current node, and next node. After each reversal, reconnect the reversed block to the list. Leave fewer than k remaining nodes unchanged. Time is O(n), space is O(1).
Q. Print all permutations of a given string.
asked 9xmediumBacktrackingTechnical2013-2020
Ans. Use backtracking to build permutations by choosing each unused character in turn, recursing until the current string has the original length, then print it. Keep a character array, a boolean used array, and a temporary result buffer. The time complexity is O(n × n!) and the recursion depth is O(n).
Q. Perform boundary traversal of a binary tree.
asked 9xmediumTreesManagerial, Technical2013-2024
Ans. Boundary traversal visits the root, the left boundary excluding leaves, all leaves from left to right, then the right boundary excluding leaves in reverse. Use recursion to collect leaves, a list for the left side, and a stack or reverse list for the right side. Time is O(n), with O(h) auxiliary space excluding output.
Q. Find the median of a stream of running integers
asked 9xmediumHeapsTechnical2014-2021
Ans. Use two heaps: a max heap for the lower half of numbers and a min heap for the upper half. Insert each new number into the correct heap, then rebalance so their sizes differ by at most one. The median is the larger heap’s top, or the average of both tops. Insert is O(log n), median is O(1).
Q. Search an element in a sorted and rotated array
asked 9xmediumBinary searchManagerial, Technical2014-2021
Ans. Use a modified binary search: compare the middle element with the ends to decide which half is sorted, then check whether the target lies inside that sorted half. If it does, search there, otherwise search the other half. For distinct elements, this takes O(log n) time and O(1) space.
Q. Find the next greater number with the same set of digits
asked 9xmediumArraysOnline test, Technical2015-2021
Ans. Scan digits from right to left to find the first digit smaller than the digit after it. Swap it with the smallest larger digit to its right, then sort or reverse the suffix into ascending order. If no such digit exists, no greater number is possible. This is the next permutation algorithm, O(n) time.
Q. Find the longest palindromic substring in a given string.
asked 9xmediumStringsOnline test, Technical2015-2020
Ans. Use expand around centres: for each index, expand once for an odd-length palindrome and once between indices for an even-length palindrome, tracking the best start and length. The key detail is handling both centre types. This uses only a few variables, runs in O(n squared) time, and uses O(1) extra space.
Q. Print a matrix in spiral order.
asked 9xeasyArraysManagerial, Technical2013-2019
Ans. Traverse the matrix layer by layer using four boundaries: top, bottom, left, and right. Print the top row, right column, bottom row, and left column, then move the boundaries inward. No extra data structure is needed apart from the output. Time complexity is O(mn), and space is O(1).
Q. Explain the difference between TCP and UDP.
asked 9xeasyNetworkingTechnical2014-2021
Ans. TCP is connection-oriented and reliable, while UDP is connectionless and faster but does not guarantee delivery. TCP orders packets, retransmits lost data, and provides flow and congestion control. UDP sends datagrams with minimal overhead, so it is useful for real-time traffic like video calls, gaming, DNS, or streaming where some loss is acceptable.
Q. Find the row with the maximum number of 1s in a binary matrix
asked 9xeasyArraysOnline test, Technical2012-2021
Ans. Start from the top right cell and move left when you see a 1, updating the answer row, or move down when you see a 0. For a row-wise sorted binary matrix, this finds the row with the most 1s in O(rows + columns) time and O(1) space.
Q. Find the Lowest Common Ancestor (LCA) in a Binary Tree.
asked 8xmediumTreesTechnical2014-2021
Ans. Use a recursive DFS: if the current node is null, p, or q, return it. Recurse into left and right. If both sides return non-null, the current node is the LCA. Otherwise return the non-null side. This uses the call stack and runs in O(n) time, with O(h) space.
Q. Perform level order traversal of a binary tree in spiral (zigzag) form.
asked 8xmediumTreesOnline test, Technical2012-2021
Ans. Use a queue for breadth first traversal and a boolean flag to alternate direction at each level. For every level, take its current size, collect node values, enqueue children left then right, and reverse the collected values when needed. This gives spiral order in O(n) time and O(w) space.
Q. Serialize and deserialize a binary tree
asked 7xmediumTreesManagerial, Technical2015-2020
Ans. Serialize the tree using preorder traversal and record null children with a sentinel, then deserialize by reading the values back in the same order. Use a list or stream of tokens and a recursive index or queue. Each node and null marker is processed once, so time is O(n) and space is O(n).
Q. Convert an infix expression to postfix expression
asked 7xmediumStacksManagerial, Online test, Technical2015-2021
Ans. Use a stack to convert infix to postfix by scanning the expression left to right and outputting operands immediately. Push opening brackets, pop until an opening bracket on closing brackets, and for operators pop higher or equal precedence operators before pushing the current one. Finally pop remaining operators. This runs in O(n) time.
Q. Design and implement an LRU (Least Recently Used) Cache.
asked 7xmediumDesignTechnical2014-2024
Ans. Use a hash map plus a doubly linked list to implement an LRU cache. The map gives O(1) access from key to node, and the list keeps usage order. On get or put, move the node to the front. When capacity is exceeded, remove the tail in O(1).
Q. Find the length of the longest substring without repeating characters.
asked 7xmediumStringsManagerial, Technical2014-2023
Ans. Use a sliding window and a hash map of each character’s most recent index to find the longest substring without repeats. Move the right pointer through the string; if a character was seen inside the current window, move the left pointer just after its previous index. Track the maximum window length. Time is O(n), space is O(k).
Q. Given a sorted dictionary of an alien language, find the order of characters.
asked 7xmediumGraphsOnline test, Technical2015-2024
Ans. Build a directed graph of character precedence and return a topological ordering of it. Compare each adjacent pair of words and add an edge from the first differing character in word one to word two. If a longer word appears before its exact prefix, the order is invalid. Use indegrees and Kahn’s algorithm. Time is O(total characters).
Q. Find the maximum path sum in a binary tree
asked 7xhardTreesTechnical2017-2021
Ans. Use a postorder DFS and keep a global best sum. For each node, compute the best downward gain from its left and right children, ignoring negative gains by taking zero. Update the global answer with node value plus both gains, then return node value plus the larger gain. Time is O(n), space is O(h).
Q. Check whether a given linked list is a palindrome.
asked 7xeasyLinked listsOnline test, Technical2014-2019
Ans. Use two pointers to find the middle, reverse the second half of the linked list, then compare it node by node with the first half. The key detail is restoring the reversed half afterwards if the list must remain unchanged. This uses constant extra space and takes O(n) time.
Q. Check whether a binary tree is a binary search tree
asked 7xeasyTreesTechnical2013-2017
Ans. Check it by doing a DFS with valid lower and upper bounds for each node. A node must be greater than its lower bound and less than its upper bound, then pass updated bounds to its children. Use recursion or an explicit stack. Time is O(n) and space is O(h).
Q. Merge overlapping intervals.
asked 6xmediumArraysOnline test, Technical2014-2022
Ans. Sort the intervals by start time, then scan once, keeping a result list of merged intervals. For each interval, compare its start with the end of the last interval in the result. If they overlap, extend the end; otherwise, append it. Time complexity is O(n log n) due to sorting, with O(n) space.
Q. Convert a given binary tree to a sum tree
asked 6xmediumTreesOnline test, Technical2013-2023
Ans. Use postorder traversal and update each node after processing its children. For every node, recursively get the sum of the original left and right subtrees, store the node’s old value, set the node’s value to left sum plus right sum, and return that plus the old value. Time is O(n), stack space is O(h).
Q. Find an element in a rotated sorted array.
asked 6xmediumBinary searchTechnical2013-2021
Ans. Use modified binary search: compare the middle element with the left and right bounds to decide which half is sorted, then check whether the target lies in that sorted half. If it does, search there; otherwise search the other half. This works in O(log n) time and O(1) space for distinct elements.
Q. Print a binary tree in vertical order traversal.
asked 6xmediumTreesOnline test, Technical2016-2021
Ans. Use level order traversal while assigning each node a horizontal distance, with root at 0, left child minus 1 and right child plus 1. Store values in a map from distance to list. BFS preserves top to bottom order within each column. Print columns from smallest to largest distance. Time is O(n).
Q. Connect nodes at the same level in a binary tree.
asked 6xmediumTreesManagerial, Online test, Technical2014-2022
Ans. Use level order traversal with a queue, linking each node to the next node removed from the same level. For each level, process exactly its current queue size, keep a previous pointer, set previous.next to current, and set the last node’s next to null. Time is O(n), space is O(width).
Q. Merge k sorted arrays into a single sorted array.
asked 6xmediumArraysTechnical2014-2020
Ans. Use a min-heap to repeatedly take the smallest current element from the k arrays and append it to the result. Store each heap entry as the value plus its array index and position, then push the next element from that array. For N total elements, time is O(N log k) and space is O(k).
Q. Perform zigzag (spiral) traversal of a binary tree
asked 6xmediumTreesOnline test, Technical2013-2021
Ans. Use level order traversal with a queue, but alternate the order in which each level’s values are recorded. For each level, process all queued nodes, add children left then right, and write values either left to right or right to left. This takes O(n) time and O(w) space, where w is maximum width.
Q. Perform zig-zag (spiral) traversal of a binary tree
asked 6xmediumTreesTechnical2013-2021
Ans. Use level order traversal with a queue, but reverse the order of values on every alternate level. Process nodes level by level, store current level values in a list, append left and right children to the queue, then add the list normally or reversed. Time complexity is O(n), space complexity is O(n).
Q. Find the distance between two given nodes in a binary tree.
asked 6xmediumTreesOnline test, Technical2014-2022
Ans. Find the lowest common ancestor of the two nodes, then compute the distance from it to each node and add those distances. Equivalently, distance equals depth(a) plus depth(b) minus twice depth(LCA). Use a DFS to find the LCA and depths. Time complexity is O(n), with O(h) recursion space.
Q. Find the first non-repeating character in a stream of characters
asked 6xmediumStringsOnline test, Technical2014-2020
Ans. Use a frequency map and a queue of candidate characters, updating both as each stream character arrives. Increment the character’s count, push it into the queue if first seen, then remove queue front items whose count is greater than one. The queue front is the current first non-repeating character. Each update is O(1) amortised time.
Q. Reverse a linked list.
asked 6xeasyLinked listsTechnical2014-2017
Ans. Reverse a linked list by iterating through it and changing each node’s next pointer to point to the previous node. Keep three pointers: previous, current, and next, so you do not lose the rest of the list. At the end, previous is the new head. Time complexity is O(n), space complexity is O(1).
Q. Evaluate a postfix expression.
asked 6xeasyStackOnline test, Technical2015-2020
Ans. Evaluate a postfix expression using a stack, scanning tokens from left to right. Push operands onto the stack; when an operator appears, pop the required operands, apply the operator, and push the result back. For binary operators, preserve operand order. The final stack value is the answer. Time complexity is O(n).
Q. Design a Parking Lot system.
asked 5xmediumObject designSystem design, Technical2015-2025
Ans. Design it with levels, parking spots by type, vehicles, tickets, gates and a payment service. Keep an in-memory or database-backed index of available spots per spot type, so entry can allocate the nearest valid spot quickly and exit can free it, calculate fees from the ticket, and update occupancy atomically.
Q. Find the roots of a quadratic equation
asked 4xeasyAlgebraOnline test2019
Ans. Compute the discriminant d = b² - 4ac, then use the quadratic formula roots = (-b ± √d) / 2a. If d is positive, there are two real roots; if zero, one repeated real root; if negative, roots are complex. Time complexity is O(1) and no extra data structure is needed.
Q. Solve a linear equation in one variable
asked 3xeasyAlgebraOnline test2019
Ans. Isolate the variable by doing the same operation to both sides of the equation. First remove brackets, then combine like terms. Move all variable terms to one side and constants to the other. Finally divide by the coefficient of the variable. Check by substituting the answer back into the original equation.
Q. Tell me about a situation where you worked on a tight deadline.
asked 3xunknownTime managementManagerial2020
Ans. Choose a real example where the deadline was important, not just busy. Explain the context, your role, the constraints, and how you prioritised. Emphasise communication, trade-offs, focus, and quality control. Interviewers listen for calm planning, ownership, teamwork, and evidence that you delivered without hiding risks or cutting critical corners.
Q. Describe a time when you had a conflict with your manager and how you handled it
asked 3xunknownConflict resolutionManagerial2014-2021
Ans. Choose a real disagreement about priorities, approach, or resources, not a personality clash. Emphasise that you stayed respectful, sought to understand your manager’s reasoning, used evidence, and focused on business outcomes. Interviewers listen for maturity, self-awareness, willingness to compromise, and whether the relationship improved or the decision became better.
Q. Describe a situation where you had a conflict with your manager and how you handled it.
asked 3xunknownConflict resolutionBehavioural, Managerial2016-2019
Ans. Choose a real disagreement about priorities, scope, quality, or approach, not a personality clash. Emphasise that you stayed respectful, listened to your manager’s reasoning, shared evidence, and looked for a business-focused solution. Interviewers listen for maturity, emotional control, openness to feedback, and the ability to disagree without damaging trust.
Q. Design a Chess game.
asked 2xmediumObject oriented designSystem design, Technical2017-2020
Ans. Design it around a Game holding an 8 by 8 Board, two Players, move history, current turn, status, and Piece objects with type, colour and movement rules. The key detail is separating legal move generation from game flow, so validation handles check, checkmate, castling, en passant and promotion consistently before applying any move.
Q. Design a notification system
asked 2xmediumDistributed systemsManagerial, System design2016
Ans. Design it as an event driven service: producers publish notification events to a queue, workers apply user preferences, render templates, and send through email, SMS, push, or in-app channels. The most important detail is reliable delivery: use durable queues, idempotency keys, retries with backoff, dead-letter queues, and delivery status tracking.
Q. Aptitude and logical reasoning problems
asked 2xeasyLogical reasoningOnline test2021
Ans. Break the problem into facts, conditions, and what must be found. Represent information with tables, diagrams, equations, or cases. Eliminate impossible options, look for patterns, and test assumptions against every condition. For quantitative questions, estimate first, then calculate carefully. For logic questions, verify that the final answer satisfies all given statements.
Q. Given a jar of pills, find the jar with defective pills.
asked 1xmediumLogical reasoningManagerial2020
Ans. Number the jars 1 to n. Take 1 pill from jar 1, 2 from jar 2, and so on, then weigh all chosen pills together once. Compare the weight with what it would be if all pills were normal. The difference, divided by the per-pill defect weight difference, gives the defective jar number.
Q. A logical puzzle about cutting a cake that is not of a regular shape.
asked 1xmediumLogical reasoningTechnical2019
Ans. Use one straight vertical cut. Pick any direction and imagine a straight line moving across the cake from one side to the other. At first it leaves no cake on one side; at the end it leaves all of it. The area changes continuously, so at some position it must leave exactly half on each side.
Showing 60 of 7,011 questions. Ranked by how often the same question came back across interviews.