unknown Interview Question for Software Developers


Country: United States




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

Very hard problem here to solve.

We could go greedy on this one or do dynamic programming.

I decided dynamic programming on the board but the more I think about it might easier and cheaper to do it in a greedy algorithm.

In a greedy we can sort the task costs and keep doing task linearly comparing what is cheaper either divide or perform linearly.

Anyway here is my dynamic programming solution:

public int MinTaskDuration(int[] tasks, int divideCost)
{	
	var taskDict = Dictionary<int, int>();
	foreach(var t in task)
	{
		if(!taskDict.ContainsKey(t))
		{
			taskDict.Add(t, 1);
		}
		else
		{
			taskDict[t]++;
		}
	}

	return MinTaskDuration(taskDic, divideCost, new Dictionary<string, int>());
}

private int MinTaskDurationCore(
	Dictionary<int, int> tasks, 
	int divideCost, 
	Dictionary<string, int> memo)
{
	var hash = CreateHash(tasks);
	if(memo.ContainsKey(hash))
		return memo[hash];
	
	if(task.Count == 0) return 0;
	if(task.Count == 1 && task.First().Value == 1) return task.First().Key;

	var tA = tasks.ToArray();

	int min = int.MaxValue;
	for(int i = 0; i < tA.Length; i++)
	{
		int a = tA[i].Key;
		if(task[tA[i].Key] == 1);
		{
			task.Remove(tA[i].Key);
		}
		else
		{
			task[tA[i].Key]--;
		}

		foreach(int j = i; j < tA.Legnth; j++)
		{
			if(!task.ContainsKey(tA[j].Key)) continue;
			int b = tA[j].Key;
			
			if(task[tA[j].Key] == 1);
			{
				task.Remove(tA[j].Key);
			}
			else
			{
				task[tA[j].Key]--;
			}

			var minDuration = MinTaskDurationCore(tasks, divideCost, memo);
			var notDividing = a + b + minDuration;
			var dividing = divideCost + Math.Max(a,b) + minDuration;

			var temp = Math.Min(notDividing, dividing);
			if(temp < min)
				min = temp;

			if(!task.ContainsKey(tA[j].Key))
			{
				task.Add(b, 1);
			}
			else
			{
				task[b]++;
			}
		}
		
		if(!task.ContainsKey(tA[i].Key))
		{
			task.Add(a, 1);
		}
		else
		{
			task[a]++;
		}
	}

	memo.Add(hash, min);
}

string CreateHash(Dictionary<int, int> tasks)
{
	StringBuilder sb = new StringBuilder();

	foreach(var kvp in tasks)
	{
		for(int i = 0; i < kvp.Value; i++)
		{
			sb.Append(kvp.Key + ',');
		}
	}

	return sb.ToString();
}

- Nelson Perez September 19, 2016 | 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