Microsoft Interview Question
SDE-2sCountry: United States
Interview Type: Phone Interview
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();
}
}
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();
}
}
Please elaborate the question. Requirement is not clear here
- naveenrai8 June 27, 2015