30 Day Risk-Free Guarantee:
100% money back if you're unsatisfied.
Book (308 Pages):
  • 150 Programming Interview Questions and Solutions
  • Five Proven Approaches to Solving Tough Algorithm Questions
  • Ten Mistakes Candidates Make -- And How to Avoid Them
  • Steps to Prepare for Behavioral and Technical Questions
  • Interview War Stories: A View from the Interviewer's Side
  • Book Preview and More Info

Video (One Hour):
  • Watch CareerCup's founder perform a totally unscripted technical interview of a candidate.
  • Overview of what interviewers look for in software engineering jobs.
  • Technical questions with white-boarding coding where the candidate does well - and struggles.
  • Interviewer reviews with what went well and poorly.
  • Video Preview and More Info

Resume Review (24 - 48hr)
  • Get your resume reviewed by a trained engineering interviewer. More Info
All Products / Services


Express Prep Package (Book)
$29.99 for e-book | $32.45 for paperback
Buy E-Book Buy on Amazon


Standard Prep Package (E-Book & Video)
Emailed Instantly | $39.99
Buy Standard

Elite Prep Package (E-Book, Video & Resume)
Emailed Instantly | $99.99
Buy Elite
 



There are two collections of objects C1 and C2. C1 can be any thing - Flowers, Fruits, Foot Balls etc.. and C2 also same. How can you find out if C1 is a subset of C2 or C2 is a subset of C1 or both are equal? Provide algorithm, code and test cases.

The interviewer was expecting a linear algorithm - the algorithm with a time complexity of O(n)

7


Nachiketha on October 08, 2008 |Edit | Edit

In thi question, C1 and C2 can have different objects at the same time. That means, C1 can have Apples, Oranges, Roses, Coke bottles, Some Integer numbers etc at the same time. Similarly, C2 can have different objects.

The interviewer was asking for test cases if all the objects were Integers. Also, the test cases for mixed collection of objects.

Anonymous on October 09, 2008 |Edit | Edit

Use hashtable to store C1 and C2. Linear time to solve the problem. the downside is that it needs extra memory. Can anybody give an in place O(n) solution?

Anonymous on October 09, 2008 |Edit | Edit

I can also think of hashtable only. But extra memory required is not O(n). It is order of types of objects present. So i think it is quite acceptable.

Anonymous on July 31, 2009 |Edit | Edit

good solution

LLOLer on August 20, 2009 |Edit | Edit


# have an associative array called assocArray[]
# Pass 1: put all the words of set C1 in the assocArray[]
# Pass 2: check if the current word from C2 is there in the array or not
* at any point if you find a word from C2 not existing in the assocArray, exit() declaring that C2 is not a subset of C1
* if the whole set C2 gets exhausted, declare C2 is a subset of C1.

Anonymous on December 07, 2009 |Edit | Edit

If we kno the complete set of possible objects...den we can have counter for each object/.....
For each element in c1 increment the corresponding counter.....
For each element if c2 decrement the corresponding counter.....
Scan counter,,..
If each element is 0 then c1=c2.
else If all counters are positive or zero....c1 is subset of c2
else If all counters are negative or zero....c1 is subset of c2
else nothing

Anonymous on March 28, 2010 |Edit | Edit

The gist of the approach is the same. Although I wonder if there's a better solution to this problem.


items1 = set([1, 3, 4])
items2 = set([4, 3, 1])

counts = dict([(item, 1) for item in items1])
for item in items2:
counts[item] = 3 if item in counts else 2

items1Subset = False; items2Subset = False
for item in counts:
if counts[item] == 2:
items1Subset = True
elif counts[item] == 1:
items2Subset = True

if items1Subset and items2Subset:
print "Items1 and Items2 are different"
elif items1Subset:
print "Items1 is subset"
elif items2Subset:
print "Items2 is subset"
else:
print "Items1 and Items2 are identical"

Add a Text Comment | Add Runnable Code
Name:
Comment:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.








Amazon (1033)Bloomberg LP (403)Qualcomm (117)Adobe (88)Goldman Sachs (78)NetApp (49)IBM (43)Morgan Stanley (33)CapitalIQ (30)Sophos (25)Achieve Internet (23)Electronic Arts (19)Motorola (18)Research In Motion (17)Flipkart (16)
Microsoft (867)Google (141)NVIDIA (98)Yahoo (82)Epic Systems (69)Expedia (47)VMWare Inc (43)Apple (32)Cisco Systems (28)Facebook (23)Infosys (22)Agilent Technologies (19)Sage Software (17)Deshaw Inc (16)FlexTrade (15)
More Companies »
Software Engineer / Dev... (1062)Financial Software Deve... (170)Testing / Quality Assur... (56)Analyst (35)Virus Researcher (25)Field Sales (15)Developer Program Engin... (9)Front-end Software Engi... (6)MyJoB (5)area sales manager (4)Assistant (3)Cabin crew (2)Accountant (1)personnel (1)Intern (1)
Software Engineer in Te... (288)Program Manager (65)Development Support Eng... (47)INTERN(MSIDC) (28)Web Developer (18)System Administrator (10)Consultant (10)Production Engineer (5)Associate Technology L2 (5)AcquireKnowledge (4)Product Security Engine... (3)Solutions Architect (2)Gamer (1)mts (1)Fresh graduate interview (0)
More Job Titles »
Algorithm (1073)Terminology & Trivia (294)C (166)Object Oriented Design (159)Java (121)Testing (114)Arrays (101)Operating System (78)Database (70)Linked List (62)String Manipulation (56)Networking / Web / Inte... (44)Threads (36)Linux Kernel (33)PHP (22)
Coding (511)C++ (204)Behavioral (159)Data Structures (155)Experience (116)Brain Teasers (111)Computer Architecture &... (79)General Questions and C... (73)Trees and Graphs (69)Math & Computation (57)Application / UI Design (45)Ideas (38)System Design (35)Puzzles (30)Bit Manipulation (20)
More Topics »
CareerCup Official Interview Book Daily Questions Requests for Help Mock Interviews Video for Cracking the Coding Interview Job Placement Service CareerCup Blog
My Profile Edit Profile & Email Settings Sign Out