SDE

200+ SDE Interview Questions and Answers

Updated 16 Dec 2024

Popular Companies

search-icon
Q1. Longest Increasing Subsequence

For a given array with N elements, you need to find the length of the longest subsequence from the array such that all the elements of the subsequence are sorted in strictly increa...read more

Q2. Return Subsets Sum to K

Given an integer array 'ARR' of size 'N' and an integer 'K', return all the subsets of 'ARR' which sum to 'K'.

Subset of an array 'ARR' is a tuple that can be obtained from 'ARR' by remov...read more

SDE Interview Questions and Answers for Freshers

illustration image
Q3. Puzzle Question

How do we measure forty-five minutes using two identical wires, each of which takes an hour to burn? We have matchsticks with us. The wires burn non-uniformly. So, for example, the two halves of ...read more

Q4. Partition to K equal sum subsets

You are given an array of 'N' integers, and a positive integer 'K'. You need to determine if it is possible to divide the array into 'K' non-empty subsets such that the sum of el...read more

Are these interview questions helpful?
Q5. Sort A “K” Sorted Doubly Linked List

You’re given a doubly-linked list with N nodes, where each node deviates at max K position from its position in the sorted list. Your task is to sort this given doubly linke...read more

Q6. Merge k sorted lists

Given 'K' sorted linked lists, each list is sorted in increasing order. You need to merge all these lists into one single sorted list. You need to return the head of the final linked list.

I...read more

Share interview questions and help millions of jobseekers 🌟

man-with-laptop
Q7. Maximum meetings

You are given the schedule of N meetings with their start time Start[i] and end time End[i]. But you have only 1 meeting room. So, you need to tell the meeting numbers you can organize in the g...read more

Q8. There is a 12 km road and a contractor who is in-charge of repairing it. Contractor updates you about the work which is done in patches. Like “Road between 3.2 km to 7.9 km repaired ”, “Road between 1.21 km to ...

read more
Ans.

The longest continuous patch of a road being repaired by a contractor is determined.

  • Iterate through the updates and keep track of the start and end points of each patch

  • Calculate the length of each patch and update the longest patch if necessary

  • Return the start and end points of the longest patch

SDE Jobs

SDE 1-8 years
Adobe Systems India Pvt. Ltd.
4.0
Noida
SDE 1-3 years
Bct Consulting
0.0
Bangalore / Bengaluru
Q9. Reverse linked list

Given a singly linked list of integers. Your task is to return the head of the reversed linked list.

For example:
The given linked list is 1 -> 2 -> 3 -> 4-> NULL. Then the reverse linked lis...read more
Frequently asked in, ,
Q10. Duplicate Subtrees

You have been given a binary tree, you are supposed to return the root values of all the duplicate subtrees. For each duplicate subtree, you only need to return the root value of any one of th...read more

Q11. Count Inversions

For a given integer array/list 'ARR' of size 'N' containing all distinct values, find the total number of 'Inversions' that may exist.

An inversion is defined for a pair of integers in the arra...read more

Q12. Boundary Traversal

You have been given a binary tree of integers. Your task is to print the boundary nodes of this binary tree in Anti-Clockwise direction starting from the root node.

NOTE:
The boundary nodes o...read more
Q13. Bipartite Graph

Given a graph, check whether the graph is bipartite or not. Your function should return true if the given graph's vertices can be divided into two independent sets, ‘U’ and ‘V’ such that every ed...read more

Q14. There are n nuts and n bolts represented in two different arrays and a function is_fit(nut_i, bolt_j) which returns 0 if its perfectly fit, 1 if it’s a tight fit and -1 if its loose fit. I was asked to arrange ...

read more
Ans.

Arrange nuts and bolts so that every nut fits perfectly with the bolt in the same position.

  • Sort both arrays in the same order using a comparison function

  • Use a binary search to find the matching bolt for each nut

  • Repeat until all nuts are matched with bolts

Q15. Given a hashmap M which is a mapping of characters to arrays of substitute characters, and an input string S, return an array of all possible mutations of S (where any character in S can be substituted with one...

read more
Ans.

Given a hashmap M and an input string S, return an array of all possible mutations of S using M's substitutes.

  • Iterate through each character in S and get its substitutes from M

  • Use recursion to generate all possible combinations of substitutes for each character

  • Time complexity: O(n^m) where n is the average number of substitutes per character and m is the length of S

  • Space complexity: O(n^m) due to the number of possible combinations

  • Optimization: Use memoization to store previo...read more

Q16. A stream of data is coming. Maintain records in a page and mechanism to see previous and next page. (Concept of Doubly Linked List) (It is always advisable to ask questions in design questions. The interviewers...

read more
Ans.

A thread is a unit of execution within a process that can run independently and concurrently with other threads.

  • Threads allow for concurrent execution of tasks within a program.

  • Threads share the same memory space and resources of the process they belong to.

  • Threads can communicate and synchronize with each other through shared data structures like locks and semaphores.

  • Threads can improve performance by utilizing multiple CPU cores or by handling I/O operations asynchronously.

  • E...read more

Q17. Given a list of integer numbers, a list of symbols [+,-,*,/] and a target number N, provide an expression which evaluates to N or return False if that is not possible. e.g. let the list of numbers be [1,5,5] an...

read more
Ans.

Given a list of numbers and symbols, provide an expression that evaluates to a target number.

  • Use recursion to try all possible combinations of numbers and symbols

  • Check for division by zero and negative numbers

  • Return False if no expression evaluates to the target number

Q18. An array A of size m+n is given whose first m elements are filled up with sorted elements. Another array B with size n filled with sorted elements. Now we have to fill all m+n elements of both array in array A ...

read more
Ans.

Merge two sorted arrays into one sorted array with one traversal.

  • Use two pointers to track the current elements in arrays A and B.

  • Compare the elements at the current pointers and insert the smaller one into array A.

  • Move the pointer of the array from which the smaller element was inserted.

  • Repeat the above steps until all elements are merged into array A.

Q19. Find the number of leaf nodes to a binary tree with 4 internal nodes?

Ans.

A binary tree with 4 internal nodes has 5 leaf nodes.

  • A binary tree with n internal nodes has n+1 leaf nodes.

  • A leaf node is a node with no children.

  • Count the number of leaf nodes to find the answer.

Q20. Given the preorder traversal of the tree is DEBFGCA and the postorder traversal of the tree is ABDECFG. Find the inorder traversal?

Ans.

Given preorder and postorder traversal, find inorder traversal of a binary tree.

  • The last element of postorder traversal is the root of the tree

  • Find the root in preorder traversal and divide the tree into left and right subtrees

  • Recursively find the inorder traversal of left and right subtrees

  • Combine the inorder traversal of left subtree, root, and inorder traversal of right subtree

Q21. Write an efficient program to count number tree structures that can be made using n number of nodes. Basically T(n)=summation (T(i) * T(n-i-1)). I used DP as there are a lot of sub-problems used again and again...

read more
Ans.

The program counts the number of tree structures that can be made using n nodes.

  • Use dynamic programming to solve the problem efficiently

  • Break down the problem into subproblems and store their solutions in an array

  • Iterate through the array to calculate the number of tree structures

  • The time complexity of the program is O(n^2)

Q22. Design a system for finding the costliest element always whenever we pick up an element from a box.(concept of Max Heap)

Ans.

Design a system using Max Heap to find the costliest element from a box when an element is picked.

  • Implement a Max Heap data structure to store the elements in the box.

  • Whenever an element is picked, the costliest element can be found at the root of the Max Heap.

  • After picking an element, update the Max Heap by removing the root and reorganizing the heap.

  • Ensure the Max Heap property is maintained during insertion and deletion operations.

  • Example: If the box contains elements [5, ...read more

Q23. What happens on server side on receiving HTTP requests and how operating system interacts and then discussion related with threading, thread pool ,synchronization, hashing etc

Ans.

When a server receives an HTTP request, it interacts with the operating system, handles threading, thread pooling, synchronization, and hashing.

  • Upon receiving an HTTP request, the server creates a new thread to handle the request.

  • The operating system manages the allocation of system resources to the server process.

  • Thread pooling is used to efficiently manage and reuse threads for handling multiple requests.

  • Synchronization mechanisms like locks or semaphores are used to ensure...read more

Q24. Make a data structure and implement an algorithm to print all the files in a directory. (the root directory can have sub-directories too.) I used an n-ary tree and BFS to print files. It can also be done using ...

read more
Ans.

Implement a data structure and algorithm to print all files in a directory, including sub-directories.

  • Use an n-ary tree or stack to represent the directory structure

  • Implement a BFS or DFS algorithm to traverse the directory and print files

  • Handle sub-directories recursively

  • Consider using a queue or stack to keep track of directories to visit

Q25. find the minimum no of jumps required to reach the end of array.where element at each index represent how many max moves you will take in right

Ans.

Find minimum number of jumps required to reach end of array with max moves in right direction at each index.

  • Use dynamic programming approach to solve the problem

  • Maintain an array to store minimum number of jumps required to reach each index

  • Traverse the array and update the minimum number of jumps for each index

  • Return the minimum number of jumps required to reach the end of array

Q26. given a 2D array of 0 and 1. where each row is sorted. find the row with maximum no of 1 in minimum time complexcity

Ans.

Find row with maximum 1s in a sorted 2D array of 0s and 1s.

  • Use binary search to find the first occurrence of 1 in each row.

  • Calculate the number of 1s in each row using the index found in step 1.

  • Keep track of the row with maximum number of 1s.

  • Time complexity: O(m log n), where m is the number of rows and n is the number of columns.

Q27. Get mth element of an stack which is filled up with n elements. where n>m without using another stack

Ans.

To get the mth element of a stack with n elements, without using another stack.

  • Create a temporary variable to store the mth element

  • Pop the top (n-m) elements from the stack and discard them

  • Pop and store the mth element in the temporary variable

  • Push back the discarded elements to the stack

  • Return the temporary variable as the result

Q28. Given an unsorted array of size 5. How many minimum comparisons are needed to find the median? then he extended it for size n

Ans.

Minimum comparisons needed to find median in unsorted array of size n

  • For odd n, median is the middle element. For even n, median is the average of middle two elements

  • Minimum comparisons needed for odd n is (n-1)/2. For even n, it is n/2

  • Various sorting algorithms can be used to find median in an unsorted array

Q29. Write a program to show whether a graph is a tree or not using adjacency matrix

Ans.

Program to determine if a graph is a tree using adjacency matrix

  • A graph is a tree if it is connected and has no cycles

  • Check if the graph is connected by performing a depth-first search or breadth-first search

  • Check if the graph has cycles by performing a depth-first search and tracking visited nodes

Q30. write a program with 2 threads. one thread should print even and other should print odd numbers in sequence. how would you make it SMP safe?

Ans.

Program with 2 threads printing even and odd numbers in sequence. How to make it SMP safe?

  • Use mutex locks to ensure only one thread accesses the shared resource (the number to be printed) at a time

  • Use condition variables to signal when it's safe for the other thread to access the shared resource

  • Use atomic variables to ensure that the shared resource is accessed atomically

  • Use thread-safe data structures to store the shared resource

  • Use thread-local storage to avoid contention b...read more

Q31. Copy fixed no of bytes from source to destination and its test cases( ex: copy(source, destination,bytes) so now command copy(a,a+3,8) will not give correct results in some cases and copy(a,a-4,8) will not give...

read more
Ans.

Copying fixed number of bytes from source to destination and its test cases.

  • Ensure source and destination are not overlapping

  • Check if the number of bytes to be copied is greater than the available space in the destination

  • Handle cases where source or destination is NULL

  • Test cases should cover all possible scenarios including edge cases

Q32. Given a sorted array which can have repeated elements, find the occurrence of an element. (Most optimal solution is O(logn) – Using binary search to find start and end occurrence)

Ans.

Given a sorted array with repeated elements, find the occurrence of a given element using binary search.

  • Use binary search to find the first occurrence of the element

  • Use binary search to find the last occurrence of the element

  • Calculate the occurrence by subtracting the indices of the last and first occurrences and adding 1

Q33. What is hashing? When is the time complexity of searching a hash table O(n)?

Ans.

Hashing is a technique to map data to a fixed-size table. Time complexity of searching a hash table is O(n) in worst case.

  • Hashing is used to store and retrieve data quickly

  • It uses a hash function to map data to a fixed-size table

  • In the best case, searching a hash table takes O(1) time

  • In the worst case, all the data maps to the same index and searching takes O(n) time

  • Collision resolution techniques like chaining and open addressing are used to handle collisions

Q34. The number of swapping operations required to sort the array [8,22,7,9,31,19,5,13] in bubblesort?

Ans.

The number of swaps required to sort an array using bubblesort

  • Bubble sort compares adjacent elements and swaps them if they are in the wrong order

  • Count the number of swaps required to sort the given array

  • The given array requires 19 swaps to be sorted using bubblesort

Q35. What data structure would you use for a dictionary?

Ans.

An array of key-value pairs is the best data structure for a dictionary.

  • Use a hash table or a balanced tree to implement the dictionary.

  • Keys should be unique and immutable.

  • Values can be any data type.

  • Access time should be O(1) or O(log n) depending on the implementation.

  • Examples: Python's dict, Java's HashMap, C++'s unordered_map.

Q36. Given an array which is alternatively sorted. What will be the time complexity to find out an element in it. e.g. 14,3,16,6,22,7,24,11,27

Ans.

Time complexity to find an element in an alternatively sorted array.

  • The time complexity will be O(log n) using binary search.

  • Check the middle element and compare with the target element.

  • If the middle element is greater than the target, search in the left half of the array.

  • If the middle element is smaller than the target, search in the right half of the array.

  • Repeat until the target element is found or the array is exhausted.

Q37. MCQ Questions

Mostly from Database, Operating Systems, Networking.

Number Of MCQs - 90

Q38. How will you describe iOS manual memory management for a new developer in few words?

Ans.

iOS manual memory management requires developers to manually allocate and deallocate memory for objects.

  • Developers must manually allocate memory for objects using methods like alloc and init.

  • Developers must also manually deallocate memory for objects using methods like release.

  • Failure to properly manage memory can lead to memory leaks and crashes.

  • ARC (Automatic Reference Counting) was introduced in iOS 5 to automate memory management.

  • However, manual memory management is still...read more

Q39. Write a program to print the powerset. E.g. given this set {1,2,3}, it will print {},{1},{2},{3},{1,2},{1,3}, {2,3}, {1,2,3}

Ans.

Program to print powerset of a given set

  • Create an empty list to store subsets

  • Loop through all possible binary numbers from 0 to 2^n-1 where n is the length of the set

  • For each binary number, convert it to binary and use the 1's as indices to select elements from the set

  • Add the selected elements to the list of subsets

  • Return the list of subsets

Q40. Describe transaction process in detail if we want to transfer from one account to other. Also design scheme for it

Ans.

The transaction process involves transferring funds from one account to another. A scheme is designed to ensure secure and accurate transfers.

  • Verify the availability of sufficient funds in the sender's account

  • Authenticate the sender's identity and authorization for the transaction

  • Deduct the transfer amount from the sender's account balance

  • Initiate a request to transfer the funds to the recipient's account

  • Validate the recipient's account details

  • Add the transferred amount to th...read more

Q41. Sort the linklist by node whose alternate nodes are already sorted

Ans.

Sort a linked list by nodes whose alternate nodes are already sorted.

  • Traverse the linked list and identify the alternate nodes.

  • Sort the alternate nodes using any sorting algorithm.

  • Merge the sorted alternate nodes back into the original linked list.

Q42. What are the differences between graph and tree?

Ans.

Graphs are non-linear data structures with cycles while trees are hierarchical data structures without cycles.

  • Graphs can have cycles while trees cannot

  • Graphs can have multiple root nodes while trees have only one

  • Graphs can have disconnected components while trees are always connected

  • Graphs can have directed or undirected edges while trees have only directed edges

  • Examples of graphs include social networks, road networks, and computer networks while examples of trees include fi...read more

Q43. What is the difference between the two declarations? int p=*(int*)i; int p=*(int*)&i;

Ans.

The first declaration casts i to int pointer and dereferences it, while the second declaration casts the address of i to int pointer and dereferences it.

  • The first declaration assumes i is already an int pointer.

  • The second declaration takes the address of i and casts it to an int pointer.

  • The first declaration may cause a segmentation fault if i is not an int pointer.

  • The second declaration may cause unexpected behavior if i is not aligned to an int.

  • Example: int i = 42; int p = ...read more

Q44. Given an array A[n] such that A[i+1] = A[i]+1 OR A[i]-1, and a number k, find out whether k is present in A[n] or not, in most efficient way?

Ans.

Efficiently check if a number is present in an array where each element differs by 1.

  • Use binary search to find the element in the array

  • Calculate the difference between the middle element and the first element

  • If the difference is equal to the index of the middle element minus the index of the first element, then the left half of the array is consecutive

  • If the difference is not equal, then the right half of the array is consecutive

  • Repeat the process on the appropriate half of t...read more

Q45. Given an expression (in single variable) like 4x+13(x-(4x+x/3)) = 9, evaluate x The expression is a string and the variable is always x

Ans.

Solve for x in a given expression with single variable.

  • Simplify the expression by applying the distributive property and combining like terms.

  • Isolate the variable term on one side of the equation and the constant terms on the other side.

  • Solve for x by dividing both sides of the equation by the coefficient of the variable term.

  • Check the solution by substituting the value of x back into the original equation.

  • In this case, simplify the expression to -5x/3 = -4 and solve for x to...read more

Q46. Merge two double linked list. What will be the difference if they are singly linked list

Ans.

To merge two double linked lists, traverse to the end of the first list and connect it to the head of the second list.

  • Traverse to the end of the first list

  • Connect the last node of the first list to the head of the second list

  • If the lists are singly linked, we need to traverse to the end of the first list and connect it to the head of the second list. But we also need to keep track of the last node of the first list to connect it to the head of the second list.

  • Example: list1: ...read more

Q47. How would you know .. your system is little endian or big endian??

Ans.

Endianess refers to the order in which bytes are stored in memory. Little endian stores the least significant byte first.

  • Check the byte order of a multi-byte integer value

  • Use a test value with known byte order to determine the system's endianess

  • Check the system's documentation or specifications

  • Use a code snippet to determine the endianess

Q48. how a function from one user process can be called in other user process?

Ans.

Inter-process communication mechanisms like pipes, sockets, message queues, shared memory can be used to call a function from one user process to another.

  • Use pipes to establish a unidirectional communication channel between two processes.

  • Use sockets to establish a bidirectional communication channel between two processes.

  • Use message queues to send messages between processes.

  • Use shared memory to share data between processes.

  • Remote Procedure Call (RPC) can also be used to call ...read more

Q49. A square picture is cut into 16 squares and they are shuffled. Write a program to rearrange the 16 squares to get the original big square

Ans.

Program to rearrange shuffled 16 squares to get original big square

  • Create a 4x4 matrix to represent the big square and fill it with shuffled squares

  • Loop through the matrix and check if each square is in the correct position

  • If a square is not in the correct position, swap it with the square in the correct position

  • Repeat until all squares are in the correct position

Q50. how function pointers are shared across different processes? using which iPCs?

Ans.

Function pointers can be shared across processes using inter-process communication mechanisms like shared memory, pipes, sockets, etc.

  • Function pointers can be stored in shared memory regions that are accessible by multiple processes.

  • Processes can communicate with each other using pipes or sockets and pass function pointers as arguments.

  • Remote Procedure Call (RPC) mechanisms can also be used to share function pointers across processes.

  • Message Passing Interface (MPI) is another...read more

1
2
3
4
5
6
Next
Interview Tips & Stories
Ace your next interview with expert advice and inspiring stories

Interview experiences of popular companies

3.7
 • 10k Interviews
3.9
 • 7.8k Interviews
4.1
 • 4.9k Interviews
4.0
 • 1.3k Interviews
3.7
 • 866 Interviews
4.4
 • 811 Interviews
3.5
 • 97 Interviews
View all

Calculate your in-hand salary

Confused about how your in-hand salary is calculated? Enter your annual salary (CTC) and get your in-hand salary

SDE Interview Questions
Share an Interview
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter