Microsoft Interview Question for SDE-2s


Country: United States




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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ConsoleApplication47
{
    class Program
    {
        static void Main(string[] args)
        {
            //Input file:
           // name begin   end
           // Bob1  1       3
           // Bob2  2       4
           // Bob3  1       4
           // Bob1  5       6
           // Bob2  4       7
           // Bob3  5       6
            string[] input_data = File.ReadAllText(@"G:\input.txt").Split(new char[] {'\n','\r','\t' }, StringSplitOptions.RemoveEmptyEntries);
            Employee[] calendar = new Employee[input_data.Length*2];
            int j = 0;
            // convert input file to the list. for each line in file, we make two items: for begin and for end
            for (int i = 0; i < input_data.Length; i++)
            {
                string[] employee = input_data[i].Split(new char[] {' ', '\r'}, StringSplitOptions.RemoveEmptyEntries);
                Employee man = new Employee(employee[0], int.Parse(employee[1]), true); // begin
                calendar[j] = man;
                j++;
                man = new Employee(employee[0],int.Parse(employee[2]), false); // end
                calendar[j] = man;
                j++;
            }
            // quicksorting
            calendar = SortingCalendar(calendar, 0, calendar.Length-1);
            int count = 0;
            int t_min=0;
            // finding when all employees are not work
            for (int i = 0; i < calendar.Length; i++)
            {
                if (calendar[i].is_begin) count++;
                else count--;
                if (count == 0)
                {
                    t_min = calendar[i + 1].time - calendar[i].time; // this line needs to be checked for i+1 < calendar.length. But i suppose, that shedule is looped
                    break;
                }
            }
            Console.WriteLine(t_min);
            Console.ReadKey();
        }
        static Employee[] SortingCalendar(Employee[] calendar, int begin, int end)
        {
            int i = begin;
            int j = end;
            int m = (begin + end) / 2;
            int median = calendar[m].time;
            while (i <= j)
            {
                while (calendar[i].time < median)
                {
                    i++;
                }
                while (calendar[j].time > median)
                {
                    j--;
                }
                if (i <= j)
                {
                    // This check is necessary for the situation where the beginning of the next cycle coincides with the end of the previous
                    if (calendar[i].time != calendar[j].time)
                    {
                        Employee temp = calendar[i];
                        calendar[i] = calendar[j];
                        calendar[j] = temp;
                    }
                    i++;
                    j--;
                }
            }
            if (j > begin) calendar = SortingCalendar(calendar, begin, j);
            if (i < end) calendar = SortingCalendar(calendar, i, end);
            return calendar;
        }
    }
    class Employee
    {
        public string name;
        public int time;
        public bool is_begin;
        public Employee()
        { }
        public Employee(string in_name, int in_time, bool in_is_begin)
        {
            name = in_name;
            time = in_time;
            is_begin = in_is_begin;
        }
    }
}

- Trushnikov.Ivan December 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assumption: Each employee has one interval (Start-i, End-i)

Define strucrure :
struct node_t {
time_t time;
int type; // START, END
}

Convert given input into following heterogenous SORTED(by time) list/array with each node of "node_t".

Now scan this list/array with counter = 0, whenever u encounter START_type "++counter" and whenever u encounter START_type "--counter".

Moment when u get counter = 0, that's free time for all employees so just check next START_type to find interval length

- sameer November 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Correction...Whenever u encounter "END_type"*** do "--counter"

- sameer November 28, 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