Tech India Line 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

get the words from paragraph by searching for blank spaces and store the words in vector or binary tree.
If you use vector then run a loop through the vector search for same words.

If use binary tree for storing words, you can arrange words around the root..example

"RAM is a boy. Ram is a Great boy"

make ROOT= "RAM"

and arrange words in binary tree like this
if(root->data>newData)
{
//insert at left
}
else if(root->data<newData)
{
//insert at right
}
else if(root->data==data)
{
//same word found, store this in some global vector
}


Hence using Binary tree will save you resource of looping for every word in vector used for storing data.

code for creating tree modify this for storing paragraph and add check for EQUALS while creating tree.

#include<iostream>
#include<string>

using namespace std;


//tree node
struct TreeNode{
	string data;
	TreeNode *leftNode;
	TreeNode *rightNode;
	TreeNode(){}
	TreeNode(string data){this->data=data;}
};


//binary tree
class BinaryTree{

	TreeNode* rootNode;
	//create binary tree
	void createTree(TreeNode*& rootNode,string data);
	void showTree(TreeNode*& root);

public:
	BinaryTree(){rootNode=NULL;}

	//starting point for creating tree, root is accessed from this func
	void insert(string data)
	{
		createTree(rootNode,data);
	}
	//show tree
	void BinaryTree::show()
	{
		showTree(rootNode);
	}

};

void BinaryTree::createTree(TreeNode*& root,string data)
{
	//check if root is NULL, this is called recursively for left or right side,for every new node
	if(root==NULL)
	{
		root=new TreeNode(data);
		root->leftNode=NULL;
		root->rightNode=NULL;
		return;
	}
	if(root->data[0]<data[0])
	{
		createTree(root->leftNode,data);
	}
	else
	{
		createTree(root->rightNode,data);
	}

}

void BinaryTree::showTree(TreeNode*& root)
{
	//check if root is NULL, this is called recursively for left or right side,for every new node
	if(root==NULL)
	{
		return;
	}
	showTree(root->leftNode);
	cout<<root->data<<endl;
	showTree(root->rightNode);
}




int main()
{
	BinaryTree tree;
	tree.insert("cd");
	tree.insert("6");
	tree.insert("asd");
	tree.insert("1");
	tree.insert("B");
	tree.insert("4");
	tree.insert("5");
	tree.insert("avs");
	tree.insert("A");
	tree.show();

	getchar();
	return 0;
}

- Nik March 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If u want count of same words and word, then use map for storage once find out the same word in tree and increment its count

map<string, int> sameWord;

- Nik March 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How abt tries?

- SuperM March 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

create a trie of the text then just use the hash table for storing the count of words.

- manfire March 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What about hashing ?
Before insertion check whether it is present before Inserting if no insert it with value 1 and if yes then increment the value with 1.

- Punit Jain March 29, 2012 | 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