Directi Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




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

#include <iostream>
#include <vector>
#include <string.h>
#include <cstring>
#include <stack>
#include <algorithm>

/*input
txyBabABA
AA
*/


using namespace std;
vector<string> pa;

typedef struct t
{
    char data;
    struct t *left;
    struct t *right;
} tree;

tree* make_node(char data)
{
    tree *t = new tree;
    t->data = data;
    t->left = NULL;
    t->right = NULL;
    return t;
}
bool check_leaf(char c)
{
    if(c>=65&&c<97)
        return false;
    else if(c>=97&&c<122)
        return true;
}

tree* build_tree(string A,int *i)
{
    tree *t = make_node(A[*i]);
    if(i<0)
        return NULL;
	//if leaf then return the node no further building of tree.
    if(check_leaf(A[*i]))
        return t;
    *i = *i-1;
    //populate right subtree
    t->right = build_tree(A,i);
    *i = *i-1;
    //populate left subtree
    t->left = build_tree(A,i);
    return t;
}
void c_path(tree *t,char *path,int *i)
{
    if(t==NULL)
        return;
    if(check_leaf(t->data))
    {
        path[*i+1] = t->data;
        path[*i+2] = '\0';
        pa.push_back(path);
        return;
    }
    else{
        *i=*i+1;
        path[*i] = t->data;
        c_path(t->left,path,i);
        c_path(t->right,path,i);
        *i=*i-1;
        return;
    }
}

int sub_search(string a,string b)
{
    int i=0,j=0;
    while(i<a.size())
    {
        while(a[i]!=b[j]&&i<a.size())
            i++;
        if(i<a.size()&&j<b.size())
        {
            i++;
            j++;
        }
        else if(i>=a.size())
            return 0;
        if(j>=b.size())
            return 1;
    }
}






void print(tree *t)
{
    if(t==NULL)
        return;
    cout<<t->data<<" ";
    print(t->left);
    print(t->right);
}

int main()
{
    string post_order,po,a;
    cin>>post_order;
    cin>>a;
    int i= post_order.size()-1;
    tree *t = build_tree(post_order,&i);
    char *p= new char[10];
    i=-1;
    c_path(t,p,&i);
    int j = pa.size()-1;
    i=0;
    int cnt=0;

    while(j>=0){
        po = pa[j--];
        //cout<<po<<endl;
        cnt+=sub_search(po,a);
    }
    cout<<cnt;
    return 0;
}

- arpitbaheti7 October 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream>
using namespace std;
int check(string path, string A)
{
	if (A.length() > path.length())
		return 0;

	int i,j=0;

	for (i = 0; i < path.length(); i++)
	{
		if (A[j] == path[i])
			j++;

		if (j == A.length())
			return 1;
	}
	return 0;
}
int explode_paths(int N, string S, int K, string A)
{
	int i, j, count = 0, c = 0;
	string path;
	vector<int> child_count;

	for (i = N - 1; i >= 0; i--)
	{
		path.push_back(S[i]);

		if (islower(S[i]))
		{
			cout << path << endl;

			c++;

			if (check(path, A))
				count++;

			path.pop_back();
			child_count.back()++;

		}
		else
			child_count.push_back(0);

		while (!child_count.empty() && child_count.back() == 2)
		{
			path.pop_back();
			child_count.pop_back();
			if (!child_count.empty())
			child_count.back()++;
		}

	}

	return count;
}
int _tmain(int argc, _TCHAR* argv[])
{
	int N, K, res;
	string S, A;
	N = 9;
	S = "txyBabABA";
	K = 2;
	A = "AA";
	cout << "Paths:" << endl << endl;
	res = explode_paths(N, S, K, A);
	cout << endl <<"No of times "<< A <<" occurs: "<< res << endl;
	return res;
}

- shivamsinha91 November 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Stack;
class Node
{
Node(char c)
{
this.c=c;
}
char c;
Node left,right;
}

class TreeProb
{
int z=0;
int explodePaths(int n, String s, int k, String a)
{
Node root=createBinary(s,n);
check(root,a);
return z;
}

void check(Node n, String s)
{
if(s.length()==0)
{
if((int)n.c>91)z++;
else
{
if(n.left!=null)check(n.left,s);
if(n.right!=null)check(n.right,s);
}
}
else
{
if(n.c==s.charAt(0))s=s.substring(1);
if(n.left!=null)check(n.left,s);
if(n.right!=null)check(n.right,s);
}
}
Node createBinary(String s, int n)
{
Stack<Node> s1 = new Stack<Node>();
Node root=null;
char c;
for(int i=0;i<n;i++)
{
Node n1=null;
if((int)(c=s.charAt(i))>91)
s1.push(new Node(c));
else
{
n1 = new Node(c);
n1.right=s1.pop();
n1.left=s1.pop();
s1.push(n1);
}
if(i==(n-1))root=n1;
}
return root;
}
}


class Tree
{
public static void main(String args[])
{
TreeProb d = new TreeProb();
System.out.println(d.explodePaths(15,"lgxBdeCAYkftKZX",1,"A"));
}
}

- Anon November 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Stack;
class Node
{
	Node(char c)
	{
		this.c=c;
	}
	char c;
	Node left,right;
}

class TreeProb
{
	int z=0;
	int explodePaths(int n, String s, int k, String a)
	{
		Node root=createBinary(s,n);
		check(root,a);
		return z;
	}
	
	void check(Node n, String s)
	{
		if(s.length()==0)
		{
			if((int)n.c>91)z++;
			else
			{
				if(n.left!=null)check(n.left,s);
				if(n.right!=null)check(n.right,s);
			}
		}
		else 
		{
			if(n.c==s.charAt(0))s=s.substring(1);
			if(n.left!=null)check(n.left,s);
			if(n.right!=null)check(n.right,s);
		}
	}
	Node createBinary(String s, int n)
	{
		Stack<Node> s1 = new Stack<Node>();
		Node root=null;
		char c;
		for(int i=0;i<n;i++)
		{
			Node n1=null;
			if((int)(c=s.charAt(i))>91)
				s1.push(new Node(c));
			else
			{
				n1 = new Node(c);
				n1.right=s1.pop();
				n1.left=s1.pop();
				s1.push(n1);
			}
			if(i==(n-1))root=n1;
		}
		return root;
	}
}


class Tree
{
	public static void main(String args[])
	{
		TreeProb d = new TreeProb();
		System.out.println(d.explodePaths(15,"lgxBdeCAYkftKZX",1,"A"));
	}	
}import java.util.Stack;
class Node
{
	Node(char c)
	{
		this.c=c;
	}
	char c;
	Node left,right;
}

class TreeProb
{
	int z=0;
	int explodePaths(int n, String s, int k, String a)
	{
		Node root=createBinary(s,n);
		check(root,a);
		return z;
	}
	
	void check(Node n, String s)
	{
		if(s.length()==0)
		{
			if((int)n.c>91)z++;
			else
			{
				if(n.left!=null)check(n.left,s);
				if(n.right!=null)check(n.right,s);
			}
		}
		else 
		{
			if(n.c==s.charAt(0))s=s.substring(1);
			if(n.left!=null)check(n.left,s);
			if(n.right!=null)check(n.right,s);
		}
	}
	Node createBinary(String s, int n)
	{
		Stack<Node> s1 = new Stack<Node>();
		Node root=null;
		char c;
		for(int i=0;i<n;i++)
		{
			Node n1=null;
			if((int)(c=s.charAt(i))>91)
				s1.push(new Node(c));
			else
			{
				n1 = new Node(c);
				n1.right=s1.pop();
				n1.left=s1.pop();
				s1.push(n1);
			}
			if(i==(n-1))root=n1;
		}
		return root;
	}
}


class Tree
{
	public static void main(String args[])
	{
		TreeProb d = new TreeProb();
		System.out.println(d.explodePaths(15,"lgxBdeCAYkftKZX",1,"A"));
	}	
}

- Anonymous November 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Stack;
class Node
{
	Node(char c)
	{
		this.c=c;
	}
	char c;
	Node left,right;
}

class TreeProb
{
	int z=0;
	int explodePaths(int n, String s, int k, String a)
	{
		Node root=createBinary(s,n);
		check(root,a);
		return z;
	}
	
	void check(Node n, String s)
	{
		if(s.length()==0)
		{
			if((int)n.c>;91)z++;
			else
			{
				if(n.left!=null)check(n.left,s);
				if(n.right!=null)check(n.right,s);
			}
		}
		else 
		{
			if(n.c==s.charAt(0))s=s.substring(1);
			if(n.left!=null)check(n.left,s);
			if(n.right!=null)check(n.right,s);
		}
	}
	Node createBinary(String s, int n)
	{
		Stack<Node> s1 = new Stack<Node>();
		Node root=null;
		char c;
		for(int i=0;i<n;i++)
		{
			Node n1=null;
			if((int)(c=s.charAt(i))>;91)
				s1.push(new Node(c));
			else
			{
				n1 = new Node(c);
				n1.right=s1.pop();
				n1.left=s1.pop();
				s1.push(n1);
			}
			if(i==(n-1))root=n1;
		}
		return root;
	}
}


class Tree
{
	public static void main(String args[])
	{
		TreeProb d = new TreeProb();
		System.out.println(d.explodePaths(15,"lgxBdeCAYkftKZX",1,"A"));
	}	
}

- Kirit November 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;

struct node
{
    char val;
    node *lc;
    node *rc;
}*root;

void setLeft(node *a,char c)
{
    node *temp=new node();
    temp->val=c;
    temp->lc=NULL;
    temp->rc=NULL;
    a->lc=temp;
}

void setRight(node *a,char c)
{
    node *temp=new node();
    temp->val=c;
    temp->lc=NULL;
    temp->rc=NULL;
    a->rc=temp;
}

void buildTree(node *root,string s,int& len)
{
    if(root->val>=65&&root->val<=90)
    {
        len--;
        setRight(root,s[len]);
        buildTree(root->rc,s,len);
        len--;
        setLeft(root,s[len]);
        buildTree(root->lc,s,len);
    }
}

void checkLeaf(node *a,int &count)
{
    if(a->val>=97&&a->val<=122)
    {
        count++;
        return;
    }
    checkLeaf(a->lc,count);
    checkLeaf(a->rc,count);
}

void explorePath(node *root,string a,int pos,int &count,int k)
{
    if(root==NULL)
    return;

    if(pos==k-1)
    {
        if(root->val!=a[pos])
        {
            explorePath(root->lc,a,pos,count,k);
            explorePath(root->rc,a,pos,count,k);
        }
        if(root->val==a[pos]&&(root->val>=97&&root->val<=122))
        {
            count++;
            return;
        }

        else if(root->val==a[pos]&&(root->val>=65&&root->val<=90))
        {
            checkLeaf(root->lc,count);
            checkLeaf(root->rc,count);
            return;
        }
    }

    else
    {if(root->val==a[pos])
    pos++;
    explorePath(root->lc,a,pos,count,k);
    explorePath(root->rc,a,pos,count,k);
    }
}

int main()
{
    int n,k;
    string s,a;
    cin>>n>>s>>k>>a;
    n--;
    root=new node();
    root->val=s[n];
    root->lc=root->rc=NULL;
    buildTree(root,s,n);
    int count=0;
    explorePath(root,a,0,count,k);
    cout<<count;
    return 0;
}

- tanu.psaxena.bce10@itbhu.ac.in November 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.


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