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);
}
}

- xinthe September 04, 2007 | Flag Reply
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!!!!!!

- xinthe September 04, 2007 | Flag Reply
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);
}

- balani.kunal September 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int maxDepthIter() {
		if (root == null)
			return 0;
		Queue<Node> q = new LinkedList<Node>();
		int max_depth = 0, depth = 0;
		q.add(root);
		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)
					q.add(n.left);
				if (n.right != null)
					q.add(n.right);
			}
			depth++;
		}
		return max_depth;
	}

- Anonymous November 13, 2012 | Flag Reply
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;
}

- nano April 01, 2015 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More