Bloomberg LP Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
I guess your code was failing because question nowhere mention or limit the no or type of item you may get as input. So your mapping should not assume that. Logic is no different from urs, it just doesnt assume anything about input items except what question mentions" " but you can assure yourself that each article can be easily categorized by description (name). " Below is the code which does not assume anything. let me know if it looks good to you:
std::map<string, int> cloth_map;
string cloth;
cout<<"Please enter cloths\n";
while(std::getline(std::cin, cloth) && cloth.empty() == false)
{
++cloth_map[cloth];
}
std::map<string, int>::iterator i = cloth_map.begin();
for(; i != cloth_map.end(); ++i)
{
string curr_cloth = (*i).first;
std::transform(curr_cloth.begin(), curr_cloth.end(), curr_cloth.begin(), ::tolower);
int count = (*i).second;
if(curr_cloth.find("sock") != string::npos)
{
if(count > 1) // handles only 1 sock
cout<<"1|"<<curr_cloth<<"\n";
if((*i).second % 2 != 0) // one sock doesnt have soulmate.:)
{
cout<<"0|"<<curr_cloth<<"\n";
}
}
else
cout<<count<<"|"<<curr_cloth<<"\n";
}
Hi I tested your code as well. It works only for the input case I have given. It does not pass all test cases.
is it a written test question ?
data structures could be :
Assumption: you know the no. of different items possible. say 15 . then create an array of that size ,
int num_of_items = 15
int [] clothes = new clothes[num_of_items];
also create one enum having all the types of cloth;
enum cloth_type
{
white_shirt =0,
red_shirt = 1
etc
}
Now read the input
while (input != eof)
{
//read cloth type from input
switch(cloth type)
{
case cloth_type.cloth_type:
clothes[cloth_type.cloth_type] ++;
sames for other cases.
}
}
after while loop iterate over array and divide the socks type of entry by 2 . like 9/2 = 4 so 4 pair and 9%2 =1 alone.
Adding to above: so it can be assumed that each items description(name) uniquely identify the item and hence can be put in map directly. Anyways I am also gonna participate in the Bloomberg online round. Can you please help me with more questions and details? It will be a great help. Thanks in advance. :)
Let's say you have 3 item types (shirts, socks and jeans).
1. Create a hashmap of type HashMap<String, Integer>(where key is category and value is its count) each for Shirt, Sock and Jeans.
2. Now, read each input line and find out category and item type(last word is item type while other words form category). Ex. if input is "polka dot sock" then item type is sock and category is "polka dot".
3. In the corresponding hashmap, increment the value of that category entry or add the new entry for the category with value = 1 if it doesn't exist.
4. Once we've read all input lines and our hashmaps are filled, first we get all the entries of sock hashmap. If and entry has even value, output it as "<value/2>|<category_name>" but if the value is odd, output 2 lines: "<value/2>|<category_name>" and "0|<category_name>".
5. Now for other categories shirt and jeans, do this:
Get the hashmap entries and output one line each for the various categories as "<value>|<category_name>".
I hope this is correct and I haven't assumed something wrongly!
public class ClothesList
{
private SortedDictionary<string, int> clothesCounts = new SortedDictionary<string, int>();
public void GetCounts(string input)
{
var clothes = input.Split(new string[] {Environment.NewLine}, StringSplitOptions.None);
foreach(string item in clothes)
{
if (!clothesCounts.ContainsKey(item))
{
clothesCounts.Add(item, 0);
}
clothesCounts[item]++;
}
}
public void PrintCounts()
{
foreach(var item in clothesCounts)
{
if (item.Key.Contains("sock"))
{
if (item.Value > 1)
{
Console.WriteLine("{0}|{1}", item.Value / 2, item.Key);
}
if (item.Value % 2 != 0)
{
Console.WriteLine("{0}|{1}", 0, item.Key);
}
}
else
{
Console.WriteLine("{0}|{1}", item.Value, item.Key);
}
}
}
}
def organize(list_of_items)
socks_count = {}
clothes_count = {}
list_of_items.each do |item|
if (item.end_with("sock")) socks_count[item] = socks_count[item].to_i + 1
else clothes_count[item] = clothes_count[item].to_i + 1
end
socks_count.keys.each do |sock|
clothes_count[sock] = socks_count[sock] / 2
socks_count[sock] = 0
end
items = (socks_count.keys.concat(other.count.keys)).sort
items.each do |item|
if others_count.has_key(item)
print others_count[item] + "|" + item
others_count.delete(item)
end
if socks_count.has_key(item)
print socks_count[item] + "|" + item
socks_count.delete(item)
end
end
end
import java.lang.*;
import java.util.HashMap;
import java.util.Hashtable;
public class LaundryCount{
public static HashMap displaylaundry(String [] clths){
HashMap laundrytable = new HashMap();
int count = 1;
for(String clth:clths){
if(laundrytable.containsKey(clth)){
count = (int) laundrytable.get(clth);
laundrytable.put(clth,count+1);
}else{
laundrytable.put(clth,count);
}
}
return laundrytable;
}
public static void main(String [] args){
LaundryCount l = new LaundryCount();
String [] clths={"white shirt",
"polka dot sock",
"red sock",
"superhero shirt",
"torn jeans",
"polka dot sock",
"white shirt",
"polka dot sock"};
HashMap clthslist = l.displaylaundry(clths);
System.out.println(clthslist);
}
}
import java.lang.*;
import java.util.HashMap;
import java.util.Hashtable;
public class LaundryCount{
public static HashMap displaylaundry(String [] clths){
HashMap laundrytable = new HashMap();
int count = 1;
for(String clth:clths){
if(laundrytable.containsKey(clth)){
count = (int) laundrytable.get(clth);
laundrytable.put(clth,count+1);
}else{
laundrytable.put(clth,count);
}
}
return laundrytable;
}
public static void main(String [] args){
LaundryCount l = new LaundryCount();
String [] clths={"white shirt",
"polka dot sock",
"red sock",
"superhero shirt",
"torn jeans",
"polka dot sock",
"white shirt",
"polka dot sock"};
HashMap clthslist = l.displaylaundry(clths);
System.out.println(clthslist);
}
}
import java.lang.*;
import java.util.HashMap;
import java.util.Hashtable;
public class LaundryCount{
public static HashMap displaylaundry(String [] clths){
HashMap laundrytable = new HashMap();
int count = 1;
for(String clth:clths){
if(laundrytable.containsKey(clth)){
count = (int) laundrytable.get(clth);
laundrytable.put(clth,count+1);
}else{
laundrytable.put(clth,count);
}
}
return laundrytable;
}
public static void main(String [] args){
LaundryCount l = new LaundryCount();
String [] clths={"white shirt",
"polka dot sock",
"red sock",
"superhero shirt",
"torn jeans",
"polka dot sock",
"white shirt",
"polka dot sock"};
HashMap clthslist = l.displaylaundry(clths);
System.out.println(clthslist);
}
}
import java.lang.*;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.TreeMap;
public class LaundryCount{
public static Map displaylaundry(String [] clths){
Map laundrytable = new TreeMap();
int count = 1;
for(String clth:clths){
if(laundrytable.containsKey(clth)){
count = (int) laundrytable.get(clth);
laundrytable.put(clth,count+1);
}else{
laundrytable.put(clth,count);
}
}
return laundrytable;
}
public static void main(String [] args){
LaundryCount l = new LaundryCount();
String [] clths={"white shirt",
"polka dot sock",
"red sock",
"superhero shirt",
"torn jeans",
"polka dot sock",
"white shirt",
"polka dot sock"};
;
System.out.println(l.displaylaundry(clths));
}
}
My initial solution wasn't passing the Judge Test Cases too.
For some reason, specifying a custom compare predicate worked for me.
https://goo.gl/4Jy3Wk
I could have sworn the default compare predicate of the STL containers worked out of the box with strings...
#include <iostream>
#include <map>
using namespace std;
// Custom types
typedef unsigned int ui;
// Custom comparators
const auto alphaCompare = [](const string& lhs, const string& rhs) {
return lexicographical_compare(
begin(lhs), end(lhs),
begin(rhs), end(rhs),
[](char l, char r) { return tolower(l) < tolower(r); });
};
// Globals
map<string, ui, decltype(alphaCompare)> clothToCount(alphaCompare);
int main() {
string cloth;
while (getline(cin, cloth))
clothToCount[cloth]++;
for (auto x : clothToCount) {
if (x.first.find("sock") != string::npos) {
if (x.second / 2 != 0)
cout << x.second / 2 << "|" << x.first << endl;
if (x.second % 2 != 0)
cout << 0 << "|" << x.first << endl;
} else cout << x.second << "|" << x.first << endl;
}
return 0;
}
Could we just use a TreeMap? We don't have to worry about the order of items by that way. When reading the input, If the item has already been in the TreeMap, we simply add the value by 1, otherwise, we put(item, 1) into the TreeMap. During we output the items, we get the last 4 characters of each key in the TreeMap to check whether that substring equals to ''sock''. If it does not, simply output the item and the number. If it does, we output depend on whether the number is even or odd.
- Anonymous November 16, 2014