Microsoft Interview Question for Interns


Country: India
Interview Type: Written Test




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

More concise way:
just compute the number of days (relative to 0th year) for both the given date and the user-input date separately. Of course needs to take care of leap years in the computation, but that is easy. Then simply do a minus to find the difference in number of days from the given date. Then play around the difference mod 7 to map to a day of week.

- tin February 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

you just have to calculate the number of days present between two dates.
For example let the date be 10 dec 2013.No of days between two date is 474(do keep note of leap year :)).Then apply modulo 7 on number of days.In this case it will be 5.add 5 to to the thursday it will become tuesday.Similar will be logic fo past date

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

Using ashishanand's logic i made this code.Works fine :-

#include <iostream>

using namespace std;

/*

Harcoded Today's Given Date 27/08/2012 
Day Thursday

*/
class date
{
      int dd,mm,yyyy;
      
public:
      
      date(int d,int m,int y)
      {dd=d;mm=m;yyyy=y;}
      
      void compute_day();
      
};

void date :: compute_day()
{
     int count_year=0,count_days=0;
     int year=yyyy,month=mm,d=dd;
     cout<<"Date is "<<d<<"/"<<month<<"/"<<year<<"\n";
     while(year != 2012)
                {       
                        if(year>2012)    // if year is greater compute odd days
                      {
                        if(year%400==0||(year%4==0&&year%100!=0))
                        count_year+=2;
                        else
                        count_year++;
                        year-=1;
                        } 
                        
                        else // if year is less compute odd days
                        {
                        
                        if(year%400==0||(year%4==0&&year%100!=0))
                        count_year-=2;
                        else
                        count_year--;
                        year+=1;
                        
                        }
                }      
     while(month != 8)
                 {
                      if(month>8)
                      {
                      if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) // if month is greater compute odd days
                      count_days+=3;
                      else if(month==2)
                      ;
                      else
                      count_days+=2;
                      month--;
                      }
                      
                      else
                      {
                      if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
                      count_days-=3;
                      else if(month==2)
                      ;
                      else
                      count_days-=2;
                      month++;
                      }
                 }                   
         if(d>27) // if days are greater compute odd days
         { count_days+=(d-27);}
         else
         { count_days-=(27-d);}
              
         int gap=(count_days+count_year)%7;
         if(gap<0) gap+=7;  //mathematical modulo                
          if(gap==0)
          cout<<"Thurday";
          else if(gap==1)
          cout<<"Friday";
          else if(gap==2)
          cout<<"Saturday";
          else if(gap==3)
          cout<<"Sunday";
          else if(gap==4)
          cout<<"Monday";
          else if(gap==5)
          cout<<"Tuesday";
          else if(gap==6)
          cout<<"Wednesday";
          else ;
          
                            
     
 }
             
int main()
{
    
    date toCheck(26,10,2016);  // change it to any date you want to check
    toCheck.compute_day();
    system("pause");
    return 0;
}

- sahil.bathla1 November 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this code is nit giving crct o/p

- anonymous November 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
4
of 4 votes

can you give the test case for which it failed

- sahil.bathla1 November 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

the code is not concise

- tin February 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Calendar;
import java.util.GregorianCalendar;

public class ParseDate {

	/**
	 * Given today is Thursday and 23rd August, 2012. Write a function to input
	 * a date (future or past) and tell which day it is:- int day_of_week(int
	 * dd, int mm, int yyyy) Mon -1, Tue - 2, Wed -3......Sun-7
	 */

	public static int day_of_week(int dd, int mm, int yyyy) {
		Calendar cal = new GregorianCalendar();
		cal.set(yyyy, mm, dd);
		
		Calendar today = new GregorianCalendar();
		today.set(2012, 7, 23);
		
		int diff = 4;
		if (today.compareTo(cal) < 0) {
			while (today.compareTo(cal) != 0) {
				today.add(Calendar.DAY_OF_YEAR, 1);
				diff = (diff + 1) % 7;
				if (diff == 0) {
					diff = 7;
				}
			}
		} else if (today.compareTo(cal) > 0) {
			while (today.compareTo(cal) != 0) {
				today.add(Calendar.DAY_OF_YEAR, -1);
				diff = (diff - 1) % 7;
				if (diff == 0) {
					diff = 7;
				}
			}
		} else {
			;
		}
		System.out.println(diff);
		return 0;
	}

	public static void main(String[] args) {
		day_of_week(1, 6, 2012);
	}

}

- Kevin March 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>

using namespace std;

/*

Harcoded Today's Given Date 27/08/2012
Day Monday

*/
class date
{
int dd,mm,yyyy;

public:

date(int d,int m,int y)
{dd=d;mm=m;yyyy=y;}

void compute_day();

};

void date :: compute_day()
{
int count_year=0,count_days=0;
int year=yyyy,month=mm,d=dd;
cout<<"Date is "<<d<<"/"<<month<<"/"<<year<<"\n";
while(year != 2012)
{
if(year>2012) // if year is greater compute odd days
{
if(year%400==0||(year%4==0&&year%100!=0))
count_year+=2;
else
count_year++;
year-=1;
}

else // if year is less compute odd days
{

if(year%400==0||(year%4==0&&year%100!=0))
count_year-=2;
else
count_year--;
year+=1;

}
}
while(month != 8)
{
if(month>8)
{
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) // if month is greater compute odd days
count_days+=3;
else if(month==2)
;
else
count_days+=2;
month--;
}

else
{
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
count_days-=3;
else if(month==2)
;
else
count_days-=2;
month++;
}
}
if(d>27) // if days are greater compute odd days
{ count_days+=(d-27);}
else
{ count_days-=(27-d);}

int gap=(count_days+count_year)%7;
if(gap<0) gap+=7; //mathematical modulo
if(gap==0)
cout<<"Tuesday";
else if(gap==1)
cout<<"wedday";
else if(gap==2)
cout<<"Thursday";
else if(gap==3)
cout<<"friday";
else if(gap==4)
cout<<"satday";
else if(gap==5)
cout<<"sunday";
else if(gap==6)
cout<<"monday";
else ;



}

int main()
{

date toCheck(19,9,2014); // change it to any date you want to check
toCheck.compute_day();
return 0;
}

- Anonymous September 19, 2013 | 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