
PubMatic


PubMatic SDE-2 Interview Questions and Answers
Q1. Binary Tree Zigzag Traversal Problem Statement
Given a Binary Tree comprised of 'N' nodes with integer values, your task is to print the zigzag traversal of the tree.
Note:
The zigzag pattern implies that the f...read more
Implement a function to print the zigzag traversal of a Binary Tree.
Traverse the Binary Tree level by level, alternating the direction of traversal for each level.
Use a queue to keep track of nodes at each level and a flag to switch the direction of traversal.
Print the values of nodes in the zigzag order as described in the problem statement.
Q2. Time to Burn Tree Problem
You are given a binary tree consisting of 'N' unique nodes and a start node where the burning will commence. The task is to calculate the time in minutes required to completely burn th...read more
Calculate the time in minutes required to completely burn a binary tree starting from a given node.
Start burning from the given node and spread fire to adjacent nodes each minute
Track the time taken for each node to burn completely
Return the maximum time taken to burn the entire tree
Q3. Trailing Zeros in Factorial Problem
Find the number of trailing zeroes in the factorial of a given number N
.
Input:
The first line contains an integer T
representing the number of test cases.
Each of the followi...read more
Count the number of trailing zeros in the factorial of a given number.
Count the number of factors of 5 in the factorial of the given number N.
Divide N by 5, then by 25, then by 125, and so on, and sum up the quotients to get the total number of trailing zeros.
Example: For N=10, 10/5=2, so there are 2 factors of 5 in 10!, hence 2 trailing zeros.
Q4. Loot Houses Problem Statement
A thief is planning to steal from several houses along a street. Each house has a certain amount of money stashed. However, the thief cannot loot two adjacent houses. Determine the...read more
Determine the maximum amount of money a thief can steal from houses without looting two consecutive houses.
Use dynamic programming to keep track of the maximum money that can be stolen up to each house.
At each house, the thief can either choose to steal from the current house or skip it and steal from the previous house.
The maximum amount of money that can be stolen without looting two consecutive houses is the maximum of the two options.
Q5. Convert Binary Tree to Mirror Tree
Convert a given binary tree into its mirror tree, where the left and right children of all non-leaf nodes are interchanged.
Input:
An integer ‘T’ denoting the number of test c...read more
Convert a binary tree into its mirror tree by interchanging left and right children of non-leaf nodes.
Traverse the tree in a recursive manner and swap the left and right children of each node.
Use a temporary variable to swap the children of each node.
Ensure to modify the tree in place without creating a new tree.
Example: For the input tree 1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1, the mirror tree will have inorder traversal as 7 4 2 1 6 3 5.
Q6. Topological Sort Problem Statement
Given a Directed Acyclic Graph (DAG) consisting of V
vertices and E
edges, your task is to find any topological sorting of this DAG. You need to return an array of size V
repr...read more
Implement a function to find any topological sorting of a Directed Acyclic Graph (DAG).
Use Depth First Search (DFS) to find the topological ordering of the vertices.
Start by visiting a vertex and recursively visit its adjacent vertices before adding it to the result array.
Maintain a visited array to keep track of visited vertices and avoid cycles.
Once all vertices are visited, reverse the result array to get the topological sort.
Example: For a DAG with edges 0->1, 0->3, 1->2,...read more

Top SDE-2 Interview Questions from Similar Companies







Reviews
Interviews
Salaries
Users/Month

