Interview Question


Country: United States




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

I think of multiple ways to solve this.
1) start traversing matrix, insert k node into binary tree and keep it balanced.Create queue and add the element in that as well.
Before adding new element, search binary tree if element with same value found thats the answer. If not then continue.
After k elements, when new elements comes,dequeue from queue, remove that element from binary search tree, search inside binary search tree for same value elements, if not found add element in queue and binary search tree as well.
Time Complexity nlong
N for traversing matrix elements (here n is total number of elements in matrix)
logn for insertion, deletion in binary search tree (insertion and search can be done on one go)

2) Same approach can be done using hash table for insertion n deletion. Hashtable will contain k elements.

- Anonymous August 11, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

LinkedHashSet (window size = k)
remove element when window reaches size k

- kn August 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think using a HashSet (java) will do the trick. It implements the Set interface so uniqueness is guaranteed.
The code below will run in worst case O(i*j) time, but I THINK this is a mandatory step anyway to read the matrix. All other functions are O(1) time.
Note: no -ve K or other error condition checks.

public boolean findDuplicateWithinIndex(int[][] input, int k)
	{
		int currentK = 0;
		for(int i = 0; i < input.length-1; i++)
		{
			for(int j=0; j < input[i].length-1; j++)
			{
				Integer cell = input[i][j];
				System.out.println(cell.hashCode());
				
				if(!hs.add(cell)) //collision
				{
					if(currentK < k) //found collision within k indices
						return true;
				}
				if(currentK < k)
					currentK++;
				else
					currentK = 0; //reset if we've surpassed K
			}
		}
		return false; //no collision within K indices.

Sample input:

int[][] findDuplicateInput = new int[][] { { 1,2,3,4} , { 5, 6, 3, 8 }, {9, 2, 11, 12 } };
		DuplicateEntriesWithinIndex duplicate = new DuplicateEntriesWithinIndex();
		int k = 9;
		boolean found = duplicate.findDuplicateWithinIndex(findDuplicateInput, k);//no collision
		if(found)
			System.out.println("Find duplicate: " + k);
		else
			System.out.println("No dupes.");

Output:
Dupe found at 9

- fayezelfar August 21, 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