Sapient Corporation Interview Question for Software Architects


Country: India
Interview Type: Written Test




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

Since catalogs can either have sub-catalog or products under it, this implies recursive nature..similar to file systems(under directories we can have directories and files).
So we can use composite pattern here.

- BJ August 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

agree. this is classical Composite pattern

- sagi.agur October 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Department
{
	public:
		Department(int numProd, char* prodNames[]);
		void addDiscount(int discount, char* prodName="");
		void addProduct(char* prodName);
		void addProducts(char* prodNames[]);
		char* name();
	private:
		HashTable or Vector of products;
}
class Catalog
{
	public:
		Catalog(int numDept, char* deptNames[]);
		void addDiscount(int discount, char* deptName, char* prodName = "");
	
	private:
		HashTable or Vector of Departments;
}

Something of the above sort can be extended as per the requirements of the design interface.

- ak3008 July 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
Though in C we cannot do much with classes, but here is my choice of data structures in C:- {{{ struct Product { char *name; char *department; double price; int quantity; }product; struct Department { char *name; char *incharge_name; int no_of_people; struct Product **items; }deptt; struct List { struct Department **a; }list; Feel free to point if there is something wrong with my code.. - Akshay Jindal July 31, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorry, posting for the first time, didn't know it will take it like this
Though in C we cannot do much with classes, but here is my choice of data structures in C:-

struct Product 
{
 char *name; 
 char *department; 
 double price;
 int quantity;
}product;

struct Department
{ 
 char *name;
 char *incharge_name;
 int no_of_people;
 struct Product **items;
}deptt;

struct List 
{ 
 struct Department **a;
}list;

Feel free to point if there is something wrong with my code..

- Akshay Jindal July 31, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;

interface Catalog {
	public void showAllProducts();
	public void addDiscount(double discountPercent);
}

class Product implements Catalog {
	private String prodId;
	private String prodName;
	private double price;

	public Product(String prodId, String prodName, double price){
		this.prodId = prodId;
		this.prodName = prodName;
		this.price = price;
	}

	@Override
	public void addDiscount(double discountPercent){
		price = price - ((discountPercent*price)/100);
	}

	@Override
	public void showAllProducts(){
		System.out.println( "\n\n Product id: " + prodId + " " + "prodName: " + 
								prodName + " price : " + price + "\n\n");
	}
}

class Department implements Catalog {
	private ArrayList<Catalog> subCatalogList = new ArrayList<Catalog>();
	private String departmentId;
	private String departmentName;

	public Department(String departmentId, String departmentName){
		this.departmentName = departmentName;
		this.departmentId = departmentId;
	}

	public void addProduct(Catalog catalog){
		subCatalogList.add(catalog);
	}

	public void addSubDepartment(Catalog catalog){
		subCatalogList.add(catalog);
	}

	@Override
	public void addDiscount(double discountPercent){
		for(Catalog subCatalog : subCatalogList){
			subCatalog.addDiscount(discountPercent);
		}
	}

	@Override
	public void showAllProducts(){
		for(Catalog subCatalog : subCatalogList){
			subCatalog.showAllProducts();
		}
	}
}

class CatalogDirectory implements Catalog {
	private ArrayList<Catalog> departmentList = new ArrayList<Catalog>();

	public void addDepartment(Catalog department){
		departmentList.add(department);
	}

	@Override
	public void addDiscount(double discountPercent){
		for(Catalog subCatalog : departmentList){
			subCatalog.addDiscount(discountPercent);
		}
	}

	@Override
	public void showAllProducts(){
		for(Catalog subCatalog : departmentList){
			subCatalog.showAllProducts();
		}
	}
}

public class CatalogDesignPattern {
	public static void main(String args[]){
		CatalogDirectory directory = new CatalogDirectory();
		Department groceries = new Department("1", "Groceries");
		Department furniture = new Department("2", "furniture");
		Department perishables = new Department("7", "Perishables");

		

		Product bed = new Product("3", "bed", 13000);
		Product almirah = new Product("4", "almirah", 15000);
		Product apple = new Product("5", "apple", 100);
		Product guava = new Product("6", "guava", 200);
		Product cheese = new Product("8", "cheese", 50);
		Product chocolate = new Product("9", "chocolate", 20);

		perishables.addProduct(chocolate);
		perishables.addProduct(cheese);

		groceries.addSubDepartment(perishables);
		groceries.addProduct(apple);
		groceries.addProduct(guava);

		groceries.addSubDepartment(perishables);

		furniture.addProduct(bed);
		furniture.addProduct(almirah);

		directory.addDepartment(groceries);
		directory.addDepartment(furniture);

		directory.showAllProducts();

		directory.addDiscount(5);

		directory.showAllProducts();

	}
}

- aakash tyagi October 12, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

IMHO this design problem can be solved by using or applying different design patterns in combination. Immediately what comes to my mind from the problem statement you can try Decorator, and Template design pattern. Decorator for catalogs having sub catalogs instead of inheritance and Template for addition of functionality at run time. Please check, a design problem might have multiple solution, the best one is based on requirement and application feasibility.

- linardni July 12, 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