Microsoft Interview Question for SDE-2s


Country: United States
Interview Type: Phone Interview




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

Please elaborate the question. Requirement is not clear here

- naveenrai8 June 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here are the following thing one has to take care when designing the bookshelf
1) : bookshelf can have different design
2) : books can be placed differently
3) : book can be edited, deleted/inserted/ and read by users at any point of time

For all these here is the design which usages SOLID design principles

BookShelf shape

public interface IBookContainerShape
{
    public void Draw(int xCoordinate, int yCoordinate);
}

public class SquareBookShelf : IBookContainerShape
{
    int height{get;set;}
    int width{get;set;}
    
    public SquareBookShelf(int height, int width)
    {
        this.height = height;
        this.width = width;
    }
    
    public void Draw(int xCoordinate, int yCoordinate)
    {
        // implement the draw function
    }
}

public class CircleBookShelf : IBookContainerShape
{
    int radius{get;set;}
    
    public SquareBookShelf(int radius)
    {
        this.radius = radius;
    }
    
    public void Draw(int xCoordinate, int yCoordinate)
    {
        // implement the draw function
    }
}

public interface IBookShelfDrawer
{
    public void DrawBookShelf();
}

public class RandomBookShelfDrawer : IBookShelfDrawer
{
    IBookContainerShape[] bookContainerShapes;
    
    public RandomBookShelfDrawer(IBookContainerShape[] bookContainerShapes)
    {
        this.bookContainerShapes = bookContainerShapes;
    }
     public void DrawBookShelf()
     {
         // draw the bookshelf by randomly putting these bookshelf designs
     }
}

public class BigToShortBookShelfDrawer : IBookShelfDrawer
{
    IBookContainerShape[] bookContainerShapes;
    
    public BigToShortBookShelfDrawer(IBookContainerShape[] bookContainerShapes)
    {
        this.bookContainerShapes = bookContainerShapes;
    }
     public void DrawBookShelf()
     {
         // draw the bookshelf by putting big bookshelf parts at bottom and shorts at top
     }
}

Books display type

public interface IDisplayContent
{
    public void DisplayContent(Book book);
}

public class PDFDisplay : IDisplayContent
{
    public void DisplayContent(Book book)
    {
        // implement the pdf book display logic
    }
}

public class WordDisplay : IDisplayContent
{
    public void DisplayContent(Book book)
    {
        // implement the word book display logic
    }
}

public class ImageDisplay : IDisplayContent
{
    public void DisplayContent(Book book)
    {
        // implement the logic of displaying the images of books pages
    }
}

Book definition

public class Book
{
    // book details, like bookID, book author, book published date....
    IDisplayContent = displayContent;
    
    public void Display()
    {
        displayContent.DisplayContent(this);
    }
}

Book placer which place book differently

public interface IBookPlacer
{
    public void BookPlace(Book);
}

public class HarizontalPlacer
{
    public void BookPlace(Book)
    {
        // implement this
    }
}

public class VerticalPlacer
{
    public void BookPlace(Book)
    {
        // implement this
    }
}

public class AngularPlacer
{
    public void BookPlace(Book)
    {
        // implement this
    }
}

And final BookShelf

public class BookShelf
{
    IBookContainerShape[] bookContainerShape;
    IBookShelfDrawer bookShelfDrawer;
    Book[] book;
    public BookShelf(IBookContainerShape[] bookContainerShape)
    {
        this.bookContainerShape = bookContainerShape;
        bookShelfDrawer = new RandomBookShelfDrawer(BookContainerShape);
    }
    
    public void Draw()
    {
        bookShelfDrawer.DrawBookShelf();
    }
    
    public void AddBooks(IBookPlacer IBookPlacer, IBookContainerShape bookContainerShape)
    {
        // we can ask draw method to return positions of each bookContainer Shape, and pass them to pace holder to place the book, also add it to book linklist.
    }
    
    public void OpenBook(int bookId)
    {
        //do book lookup and call Book.DisplayContent();
    }
}

- sonesh June 29, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Here is the list of functionalities bookshelf should have
1) : different design
2) : different ways user can put books in
3) : allow users to store more books, delete them, read them

This design uses SOILD principles
First we define the shape of bookshelf

public interface IBookContainerShape
{
    public void Draw(int xCoordinate, int yCoordinate);
}

public class SquareBookShelf : IBookContainerShape
{
    int height{get;set;}
    int width{get;set;}
    
    public SquareBookShelf(int height, int width)
    {
        this.height = height;
        this.width = width;
    }
    
    public void Draw(int xCoordinate, int yCoordinate)
    {
        // implement the drow function
    }
}

public class CircleBookShelf : IBookContainerShape
{
    int radius{get;set;}
    
    public SquareBookShelf(int radius)
    {
        this.radius = radius;
    }
    
    public void Draw(int xCoordinate, int yCoordinate)
    {
        // implement the drow function
    }
}

public interface IBookShelfDrawer
{
    public void DrawBookShelf();
}

public class RandomBookShelfDrawer : IBookShelfDrawer
{
    IBookContainerShape[] bookContainerShapes;
    
    public RandomBookShelfDrawer(IBookContainerShape[] bookContainerShapes)
    {
        this.bookContainerShapes = bookContainerShapes;
    }
     public void DrawBookShelf()
     {
         // draw the bookshelf by randomly putting these bookself designs
     }
}

public class BigToShortBookShelfDrawer : IBookShelfDrawer
{
    IBookContainerShape[] bookContainerShapes;
    
    public BigToShortBookShelfDrawer(IBookContainerShape[] bookContainerShapes)
    {
        this.bookContainerShapes = bookContainerShapes;
    }
     public void DrawBookShelf()
     {
         // draw the bookshelf by putting big bookshelfs parts at bottom and shorts at top
     }
}

Now lets define how a book content displayed

public interface IDisplayContent
{
    public void DisplayContent(Book book);
}

public class PDFDisplay : IDisplayContent
{
    public void DisplayContent(Book book)
    {
        // implement the pdf book display logic
    }
}

public class WordDisplay : IDisplayContent
{
    public void DisplayContent(Book book)
    {
        // implement the word book display logic
    }
}

public class ImageDisplay : IDisplayContent
{
    public void DisplayContent(Book book)
    {
        // implement the logic of displaying the images of books pages
    }
}

Book definition

public class Book
{
    // book details, like bookID, book auther, book published date....
    IDisplayContent = displayContent;
    
    public void Display()
    {
        displayContent.DisplayContent(this);
    }
}

Lets decide how a book can be placed to bookshelf

public interface IBookPlacer
{
    public void BookPlace(Book);
}

public class HarizontalPlacer
{
    public void BookPlace(Book)
    {
        // implement this
    }
}

public class VerticalPlacer
{
    public void BookPlace(Book)
    {
        // implement this
    }
}

public class AngularPlacer
{
    public void BookPlace(Book)
    {
        // implement this
    }
}

Here is the final bookshelf

public class BookShelf
{
    IBookContainerShape[] bookContainerShape;
    IBookShelfDrawer bookShelfDrawer;
    Book[] book;
    public BookShelf(IBookContainerShape[] bookContainerShape)
    {
        this.bookContainerShape = bookContainerShape;
        bookShelfDrawer = new RandomBookShelfDrawer(BookContainerShape);
    }
    
    public void Draw()
    {
        bookShelfDrawer.DrawBookShelf();
    }
    
    public void AddBooks(IBookPlacer IBookPlacer, IBookContainerShape bookContainerShape)
    {
        // we can ask draw method to return positions of each bookContainer Shape, and pass them to pace holder to place the book, also add it to book linklist.
    }
    
    public void OpenBook(int bookId)
    {
        //do book lookup and call Book.DisplayContent();
    }
}

- Anonymous June 29, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Please Consider the solution give by @sonesh, this was accidentaly written by me

- sonesh June 29, 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