ServiceNow
20+ MN Dastur & Company Interview Questions and Answers
Q1. Set Matrix Zeros Problem Statement
Given an N x M
integer matrix, if an element is 0, set its entire row and column to 0's, and return the matrix. Specifically, if a cell has a value 0 (i.e., matrix[i][j] == 0
)...read more
Q2. Pascal's Triangle Problem Statement
You are provided with an integer N
. The objective is to return a 2-dimensional list representing Pascal’s triangle up to row N
.
A Pascal's triangle is a triangular array wher...read more
Q3. Pythagorean Triplets Detection
Determine if an array contains a Pythagorean triplet by checking whether there are three integers x, y, and z such that x2 + y2 = z2 within the array.
Input:
The first line contai...read more
Q4. K-th Smallest Element in BST
Your task is to find the ‘K-th’ smallest element in a given Binary Search Tree (BST).
Explanation:
A Binary Search Tree is a binary tree in which for each node, all elements in the ...read more
Q5. Find product of each element of an array except that element in O(N) time complexity without using / operation
Find product of each element of an array except that element in O(N) time complexity without using / operation
Use prefix and suffix products
Multiply prefix and suffix products for each element to get the final product
Handle edge cases where array has 0 or 1 element separately
Q6. Design a system for putting newspapers using classes and functions taking different aspects into account
Design a system for putting newspapers using classes and functions
Create a Newspaper class with attributes like title, date, and content
Create a Publisher class with methods to publish and distribute newspapers
Create a Subscriber class with methods to subscribe and receive newspapers
Use inheritance to create different types of newspapers like daily, weekly, etc.
Implement a database to store newspaper information and handle subscriptions
Q7. Median of 2 sorted arrays in O(log N) time complexity and O(1) space complexity
Find median of 2 sorted arrays in O(log N) time complexity and O(1) space complexity
Use binary search to find the partition point in both arrays
Calculate the median based on the partition point and array sizes
Adjust the partition points based on the median value
Repeat until the partition points are at the median
Handle edge cases such as empty arrays and uneven array sizes
Q8. Shortest path between 2 points in 2-D space in O(log N) time
There is no known algorithm to find shortest path in 2-D space in O(log N) time.
The best known algorithm for finding shortest path in 2-D space is Dijkstra's algorithm which has a time complexity of O(N^2).
Other algorithms like A* and Bellman-Ford have better time complexity but still not O(log N).
If the points are on a grid, Lee algorithm can be used which has a time complexity of O(N).
Q9. Reverse level order traversal of a tree using Queue
Reverse level order traversal of a tree using Queue
Create a queue and push the root node into it
While the queue is not empty, pop the front node and push its children into the queue
Add the popped node to a stack
Once the queue is empty, pop elements from the stack and print them
Q10. Difference between Floyd Warshall and Djikstra
Floyd Warshall finds shortest path between all pairs of vertices while Djikstra finds shortest path from a single source.
Floyd Warshall is used for dense graphs while Djikstra is used for sparse graphs.
Floyd Warshall has a time complexity of O(n^3) while Djikstra has a time complexity of O((n+m)logn).
Floyd Warshall can handle negative edge weights while Djikstra cannot.
Floyd Warshall can detect negative cycles while Djikstra cannot.
Q11. Level order traversal of a tree using Queue
Level order traversal of a tree using Queue
Create a queue and add the root node to it
While the queue is not empty, remove the front node and print its value
Add the left and right child nodes of the removed node to the queue
Repeat until the queue is empty
Q12. Recursively deleting linked list from end
Recursively delete a linked list from the end.
Start from the head and recursively traverse to the end of the list.
Delete the last node and set the second last node's next pointer to null.
Repeat until the entire list is deleted.
Use a recursive function to implement the deletion process.
Q13. Strings Anagram in O(1) space complexity
Anagram of strings in O(1) space complexity
Use a fixed size array of integers to store the frequency of characters in the first string
Iterate through the second string and decrement the frequency of each character in the array
If all the frequencies are zero, then the strings are anagrams
Return true or false accordingly
Q14. BFS and DFS Difference
BFS and DFS are graph traversal algorithms. BFS explores nodes level by level while DFS explores nodes depth by depth.
BFS uses a queue while DFS uses a stack or recursion.
BFS is optimal for finding shortest path while DFS is optimal for finding a path between two nodes.
BFS requires more memory as it stores all the nodes at each level while DFS requires less memory.
BFS can be used to find connected components while DFS can be used to detect cycles in a graph.
Q15. Recursively deleting linked list
Recursively delete a linked list
Create a recursive function that takes the head of the linked list as input
Base case: if the head is null, return
Recursively call the function with the next node as input
Delete the current node
Q16. Write topological sort in directed acyclic graph
Topological sort is a linear ordering of vertices in a directed acyclic graph where for every directed edge uv, vertex u comes before vertex v.
Create a list to store the topological ordering of vertices.
Find a vertex with no incoming edges and add it to the list.
Remove the vertex and its outgoing edges from the graph.
Repeat the process until all vertices are added to the list.
Q17. Recursively deleting from end
Recursively delete elements from the end of an array.
Create a recursive function that removes the last element of the array.
Call the function recursively until the desired number of elements are removed.
Handle edge cases such as empty arrays and removing more elements than the array contains.
Q18. Recursively deleting tree
Recursively delete a tree by deleting all its child nodes and then the parent node.
Start from the leaf nodes and delete them first.
Then move up to the parent nodes and delete them.
Repeat until the root node is deleted.
Use post-order traversal to ensure child nodes are deleted before parent nodes.
Q19. N meetings Find sub Array sum 0
Given an array of N meetings, find a subarray with sum 0.
Use a hash table to store the cumulative sum of the array elements.
If the same sum is encountered again, it means the subarray between the two indices has a sum of 0.
Handle edge cases like when the subarray starts from index 0 or when the subarray ends at the last index.
Q20. write an algo for tower on hanoi in 7 steps
The Tower of Hanoi is a classic problem that involves moving disks from one peg to another, following specific rules.
Start by moving the top n-1 disks from the source peg to the auxiliary peg.
Move the largest disk from the source peg to the target peg.
Move the n-1 disks from the auxiliary peg to the target peg.
Repeat the process recursively for the n-1 disks on the auxiliary peg.
Continue until all disks are moved to the target peg.
Q21. Low level Design of hashmap
Hashmap is a data structure that stores key-value pairs and allows for quick retrieval of values based on keys.
Hashmap is typically implemented using an array of linked lists or a balanced binary search tree.
Each key is hashed to determine its index in the array, where the corresponding value is stored.
Collision handling is important in hashmap design to address cases where multiple keys hash to the same index.
Hashing function should be efficient to distribute keys evenly acr...read more
Interview Process at MN Dastur & Company
Top Software Engineer Interview Questions from Similar Companies
Reviews
Interviews
Salaries
Users/Month