FactSet Research Systems, Inc Interview Question for Software Engineer / Developers

Comment hidden because of low score. Click to expand.
0
of 0 vote

global variable maxdepth = 0;

void findMaxDepth()
{
MaxDepth(root, -1);
}

void find MaxDepth(class node *ptrNode, int depth)
{
if(ptrNode == null) {return;}
depth++;
if(maxDepth < depth)
{
maxDepth = depth;
}
if(ptrNode->Left!=null)
{
MaxDepth(ptrNode->left, depth);
}
if(ptrNode->Right!=null)
{
MaxDepth(ptrNode->Right, depth);
}
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

simulate the same with stack...

while storing the pointer to the node in the stack, please maintain the corresponding depth also in another stack and always maintain the correspondence with its node!!!!!!

Comment hidden because of low score. Click to expand.
0
of 0 vote

public int MaxDepth(Node root){
if(root == null){
return 0;
}

return (Math.max(MaxDepth(root.left), MaxDepth(root.right)) + 1);
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````public int maxDepthIter() {
if (root == null)
return 0;
int max_depth = 0, depth = 0;
depth++;
while(!q.isEmpty()) {
int count = q.size();
for (int i = 0; i < count; i++) {
Node n = q.remove();
if (n.left == null && n.right == null) {
if(max_depth < depth)
max_depth = depth;
continue;
}
if (n.left != null)
if (n.right != null)
}
depth++;
}
return max_depth;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

a c++ solution

``````#include <iostream>
#include <utility>
#include <queue>

using namespace std;

struct TreeNode{
int val;
TreeNode *left, *right;
TreeNode(int x):val(x){}
};

int maxDepth(TreeNode *root){
if(!root)
return 0;

return 1+max(maxDepth(root->left),maxDepth(root->right));
}

int maxDepth1(TreeNode *root){
if(!root)
return 0;

queue<pair<TreeNode *,int> > to_process;
to_process.push(make_pair(root,1));
int max_depth=1;
while(!to_process.empty()){
pair<TreeNode*,int> cur = to_process.front();
to_process.pop();
max_depth=max(max_depth,cur.second);
if(cur.first->left)
to_process.push(make_pair(cur.first->left,cur.second+1));
if(cur.first->right)
to_process.push(make_pair(cur.first->right,cur.second+1));
}
return max_depth;
}

int main(void){
TreeNode *root=new TreeNode(0);
root->left=new TreeNode(0);
root->left->left=new TreeNode(0);
root->left->left->left=new TreeNode(0);
root->right=new TreeNode(0);
root->right->left=new TreeNode(0);
cout<<maxDepth(root)<<maxDepth1(root)<<endl;
cout<<maxDepth(root->right)<<maxDepth1(root->right)<<endl;
return 0;
}``````

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.