Amazon Interview Question for SDE1s


Country: United States




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

class List
{
	DominoTile *Head;
	DominoTile *Tail;
public:

	List()
	{
		Head = NULL;
		Tail = NULL;
	}
	~List()
	{
		delete Head;
		delete Tail;
	}
	DominoTile* GetHead()
	{
		return Head;
	}
	DominoTile* GetTail()
	{
		return Tail;
	}
	void Append(DominoTile *Cnode)
	{
		if (Head == NULL)
		{
			Head = Cnode;
			Tail = Cnode;

		}
		else
		{
			Tail->Pnext = Cnode;
			Tail = Tail->Pnext;
		}


	}
	
};

class DominoTile
{
	int Ftile;
	int Etile;
 
	
	
public:
	DominoTile *Pnext;
	
	DominoTile()
	{
		Ftile = -1;
		Etile = -1;
		Pnext = NULL;
	
	}
	
	DominoTile(int FValue, int Evalue)
	{
		Ftile = FValue;
		Etile = Evalue;
		Pnext = NULL;
	}
	~DominoTile()
	{
		
		delete Pnext;
	}
	void setFtile(int Value)
	{
		Ftile = Value;
	}
	void setEtile(int Value)
	{
		Etile = Value;
	}
	int getFtile()
	{
		return Ftile;
	}
	int getEtile()
	{
		return Etile;
		
	}
	DominoTile operator= (DominoTile V)
	{
		V.Ftile = Ftile;
		V.Etile = Etile;
		V.Pnext = Pnext;
	}
	

};
void main()
{
	
	List  Tiles;
	int Fvalue;
	int EValue;
	for (int i = 0; i < 28; i++)
	{

		cout << "Please Enter Tiles";
		cin >> Fvalue;
		cin >> EValue;

		DominoTile *Tile = new DominoTile(Fvalue, EValue);
		
		if (Fvalue == -1 && EValue == -1)
		{
			break;
		}
		Tiles.Append(Tile);
	}
	DominoTile *Ptrav= new DominoTile();
	DominoTile *Ptrav2 = new DominoTile();
	Ptrav = Tiles.GetHead();
	Ptrav2 = Ptrav->Pnext;
	int LOOPFlag = 0;
		while (Ptrav2->Pnext!=NULL)
		{
			if (Ptrav->getEtile() == Ptrav2->getFtile())
			{
				LOOPFlag = 1;
				
				
			}
			else
			{
				LOOPFlag = -1;
				break;
			}
			Ptrav = Ptrav2;
			Ptrav2 = Ptrav2->Pnext;
		}
		if (Tiles.GetHead()->getFtile() == Tiles.GetTail()->getEtile())
		{
			LOOPFlag += 1;
		}
		cout << LOOPFlag;



}

- Farida December 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class List
{
	DominoTile *Head;
	DominoTile *Tail;
public:

	List()
	{
		Head = NULL;
		Tail = NULL;
	}
	~List()
	{
		delete Head;
		delete Tail;
	}
	DominoTile* GetHead()
	{
		return Head;
	}
	DominoTile* GetTail()
	{
		return Tail;
	}
	void Append(DominoTile *Cnode)
	{
		if (Head == NULL)
		{
			Head = Cnode;
			Tail = Cnode;

		}
		else
		{
			Tail->Pnext = Cnode;
			Tail = Tail->Pnext;
		}


	}
	
};

class DominoTile
{
	int Ftile;
	int Etile;
 
	
	
public:
	DominoTile *Pnext;
	
	DominoTile()
	{
		Ftile = -1;
		Etile = -1;
		Pnext = NULL;
	
	}
	
	DominoTile(int FValue, int Evalue)
	{
		Ftile = FValue;
		Etile = Evalue;
		Pnext = NULL;
	}
	~DominoTile()
	{
		
		delete Pnext;
	}
	void setFtile(int Value)
	{
		Ftile = Value;
	}
	void setEtile(int Value)
	{
		Etile = Value;
	}
	int getFtile()
	{
		return Ftile;
	}
	int getEtile()
	{
		return Etile;
		
	}
	DominoTile operator= (DominoTile V)
	{
		V.Ftile = Ftile;
		V.Etile = Etile;
		V.Pnext = Pnext;
	}
	

};
void main()
{
	
	List  Tiles;
	int Fvalue;
	int EValue;
	for (int i = 0; i < 28; i++)
	{

		cout << "Please Enter Tiles";
		cin >> Fvalue;
		cin >> EValue;

		DominoTile *Tile = new DominoTile(Fvalue, EValue);
		
		if (Fvalue == -1 && EValue == -1)
		{
			break;
		}
		Tiles.Append(Tile);
	}
	DominoTile *Ptrav= new DominoTile();
	DominoTile *Ptrav2 = new DominoTile();
	Ptrav = Tiles.GetHead();
	Ptrav2 = Ptrav->Pnext;
	int LOOPFlag = 0;
		while (Ptrav2->Pnext!=NULL)
		{
			if (Ptrav->getEtile() == Ptrav2->getFtile())
			{
				LOOPFlag = 1;
				
				
			}
			else
			{
				LOOPFlag = -1;
				break;
			}
			Ptrav = Ptrav2;
			Ptrav2 = Ptrav2->Pnext;
		}
		if (Tiles.GetHead()->getFtile() == Tiles.GetTail()->getEtile())
		{
			LOOPFlag += 1;
		}
		cout << LOOPFlag;



}

- Farida December 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Dominoes can be presented as a graph with vertices 0, 1, ..., N, where edges stand for edges connecting vertices of the corresponding number. The degree of each vertex is N+2 (N edges connecting each vertex with other vertices and one loop edge, which increases degree by 2). Building the full dominoes loop is equivalent to finding Eulerian cycle in the graph. And the latter is possible if and only if every vertex has even degree, and all of its vertices with nonzero degree belong to a single connected component. That means that we just need to check if N is even. In this case N+2 is also even which means that there's a Eulerian cycle in the graph.

- vladimir.grebennikov December 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your answer is correct if a tile is allowed to be visited more than twice. The provided example isn't making this very clear. If you think about a traditional domino game, tiles can't be connected to more than two tiles so "inner loops" aren't allowed. This then turns into the Hamiltonian cycle problem, which is NP-complete. It's possible that this specific question solution is not NP-complete as some graphs (e.g. a complete graph) can be determined to have a Hamiltonian cycle without exhaustive search.

- Omri.Bashari May 09, 2015 | Flag


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