Microsoft Interview Question
Software DevelopersTeam: Cloud + Enterprise
Country: United States
Interview Type: Written Test
public static void TransferMoveInformation(IDataReader movieDataReader, MovieInformationDataBaseAccess movieInformationDataBaseAccess)
{
int movieNameOrdinal = movieDataReader.GetOrdinal("MovieName");
int contributorTypeOrdinal = movieDataReader.GetOrdinal("ContributorType");
int contributorNameOrdinal = movieDataReader.GetOrdinal("ContributorName");
string previousMovieName = string.Empty;
MovieInformation newMovieInformation = null;
while(movieDataReader.Read())
{
string nextMovieName = movieDataReader.GetString(movieNameOrdinal);
if(!nextMovieName.Equals(previousMovieName, StringComparison.CultureInvariantIgnoreCase))
{
if(newMovieInformation != null)
{
// check if all the objects are filled, if not we can log it to or not insert it, this action should be taken by the business
movieInformationDataBaseAccess.SaveMovieInformation(newMovieInformation)
}
newMovieInformation = new MovieInformation();
newMovieInformation.Name = nextMovieName;
previousMovieName = nextMovieName;
}
string nextContributorType = movieDataReader.GetString(contributorTypeOrdinal);
string nextContributorName = movieDataReader.GetString(contributorNameOrdinal);
if(nextContributorType.Euqals("Director", StringComparison.CultureInvariantIgnoreCase))
{
if(string.IsEmptyOrNull(newMovieInformation.Director))
{
newMovieInformation.Director = nextContributorName;
}
else if(!newMovieInformation.Director.Equals(nextContributorName, StringComparison.CultureInvariantIgnoreCase))
{
// this is also an error situation, one should either log them or let business take the action on these
}
}
else if(nextContributorType.Euqals("Actor", StringComparison.CultureInvariantIgnoreCase)
{
if(newMovieInformation.Actors == null)
{
newMovieInformation.Actors = new List<string>(){nextContributorName};
}
else
{
bool isNotAlreadyPresent = true;
foreach(string actor in newMovieInformation.Actors)
{
if(actor.Equals(nextContributorName, StringComparison.CultureInvariantIgnoreCase))
{
isAlreadyPresent = false;
}
}
if(isAlreadyPresent)
{
newMovieInformation.Actors.Add(nextContributorName);
}
}
}
else
{
// we can log this as well to tell that we have some extra information which we are losing or we are having some corrupted data, we can even do some string partial match to decide if it is close to director or actor and take actions based upon that
}
}
}
- Anonymous March 21, 2015