Bloomberg LP Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

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 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

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";
	}

- Anonymous November 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi I tested your code as well. It works only for the input case I have given. It does not pass all test cases.

- xyz_coder November 15, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Perhaps this code does not sort the output in alphabetical order?

- Anonymous December 04, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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.

- code.guruji November 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It was for online Interview. I used similar logic, but it didn't pass all test cases. The test cases which failed are not known.

- xyz_coder November 15, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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. :)

- Anonymous November 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@xyz_coder : where r u testing it online ? Is somewhere Bloomberg interview test happening online? Pls share details.

- natural November 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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!

- ashu1.220791 November 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
			}
		}
	}
}

- DavidDeMar November 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@XYZ_Coder: Can you please provide the test case for which the code is failing. How are you testing it?

- Anonymous November 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- tkang1 January 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

topological sort

- codegirl March 30, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
}
}

- Sudheer January 01, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
}
}

- Sudheer January 01, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
}

}

- Sudheer January 01, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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));
}

}

- Sudheer January 01, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
}

- henriqueferrolho October 15, 2016 | 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