Amazon Interview Question
Applications DevelopersCountry: India
Interview Type: In-Person
public enum ProductCategory
{
Home, Daily, Food
}
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public ProductCategory ProductCategory { get; set; }
}
public class Review
{
public int Id { get; set; }
public int ProductId { get; set; }
public string Comment { get; set; }
public int UserId { get; set; }
public DateTime ReviewAt { get; set; }
public int Rating { get; set; }
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
}
public interface IReviewActivity
{
void PostReview(Review Rev);
void DeleteReview(Review Rev, User User);
}
public class ReviewActivity
{
List<Review> _review = new List<Review>();
public ReviewActivity()
{
}
public void PostReview(Review Rev)
{
_review.Add(Rev);
}
public void DeleteReview(Review Rev, User User)
{
if (_review.Where(f => f.UserId == User.Id).Count() > 0)
{
_review.Remove(Rev);
}
else
throw new Exception("Invalid Action");
}
public List<Review> GetReviewByProductId(int Id)
{
return _review.Where(f => f.ProductId == Id).OrderBy(f=>f.ReviewAt).Take(10).ToList();
}
public int GetAverageRatingByProductId(int Id)
{
int Count = _review.Where(f => f.ProductId == Id).Count();
if (Count != 0)
return _review.Where(f => f.ProductId == Id).Sum(f => f.Rating) / Count;
else
return 0;
}
}
public enum ProductCategory
{
Home, Daily, Food
}
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public ProductCategory ProductCategory { get; set; }
}
public class Review
{
public int Id { get; set; }
public int ProductId { get; set; }
public string Comment { get; set; }
public int UserId { get; set; }
public DateTime ReviewAt { get; set; }
public int Rating { get; set; }
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
}
public interface IReviewActivity
{
void PostReview(Review Rev);
void DeleteReview(Review Rev, User User);
}
public class ReviewActivity
{
List<Review> _review = new List<Review>();
public ReviewActivity()
{
}
public void PostReview(Review Rev)
{
_review.Add(Rev);
}
public void DeleteReview(Review Rev, User User)
{
if (_review.Where(f => f.UserId == User.Id).Count() > 0)
{
_review.Remove(Rev);
}
else
throw new Exception("Invalid Action");
}
public List<Review> GetReviewByProductId(int Id)
{
return _review.Where(f => f.ProductId == Id).OrderBy(f=>f.ReviewAt).Take(10).ToList();
}
public int GetAverageRatingByProductId(int Id)
{
int Count = _review.Where(f => f.ProductId == Id).Count();
if (Count != 0)
return _review.Where(f => f.ProductId == Id).Sum(f => f.Rating) / Count;
else
return 0;
}
}
Using C++ since that;s my favorite language but hope you get the idea.
There are various things you need to look into here....
First of all the requirements of Reviewing a product i.e, various things customers should be able to do:
Let;s assume that he could do following things
a) Rate with star rating for the product.
b) Add a Review which is just a char array of limited size.
c) edit a review.
Now each product will have a Review object attached to it.
let's say for simplicity that review object looks like
- AJ Gauravdeep September 07, 2012