Open Side Menu Go to the Top
Register
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

04-20-2015 , 10:52 PM
Quote:
Originally Posted by kerowo
Can you use the int as an index in another array, adding one to that index as you iterate over the first array? Then find the highest value in the second array. Or hash or dictionary whichever is appropriate?
hash would be appropriate for the general problem since the lookup times are faster than an array
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-20-2015 , 11:19 PM
Quote:
Originally Posted by kerowo
Can you use the int as an index in another array, adding one to that index as you iterate over the first array? Then find the highest value in the second array. Or hash or dictionary whichever is appropriate?
Yeah I think that's basically what I did.

Spoiler:
Code:
var arr = [0, 1, 1, 3, 2, 3, 1, 1];
var counts = {};
var maxCount = 0;
var maxItem;

arr.forEach(function(item) {
  if (counts[item]) counts[item]++
  else counts[item] = 1;
 
  if (counts[item] > maxCount) {
    maxCount = counts[item];
    maxItem = item;
  }
}

console.log(maxItem);
I feel like there's probably something cleaner using map/reduce. But I was nervous enough on this that I wasn't going to go down that road. If he would have started with the softball oral questions, then got into the code I probably would have been able to think more clear.

I kept trying to figure out a way to combine maxCount and maxItem into one variable (hash or?). But extracting the item value when needed while also being able to compare the counts was ugly. The last line would have been something like console.log(Object.keys(maxHash)[0]). Barf.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-21-2015 , 12:48 AM
Quote:
Originally Posted by suzzer99
Yeah I think that's basically what I did.
Did you ask if they were checking for the case if there is multiple numbers with equal repeating times.

Also javascript will round the answer if you have large numbers in your array and you won't get the exact number.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-21-2015 , 12:58 AM
Yeah if it's tied as long as you returned one of the top #s that's ok.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-21-2015 , 02:35 AM
If you want to maintain some additional state information, you can add an optimization of breaking out of the array iteration early when you know one value has such a large lead no other value can catch up.


Probably not worth implementing on your first run through the problem ( better to make sure you get a working solution and not confuse the person interviewing you), but mentioning these sort of things after you have finished can't hurt. Some some more advanced thinking.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-21-2015 , 03:10 AM
Quote:
Originally Posted by suzzer99
Yeah I think that's basically what I did.

Spoiler:
Code:
var arr = [0, 1, 1, 3, 2, 3, 1, 1];
var counts = {};
var maxCount = 0;
var maxItem;

arr.forEach(function(item) {
  if (counts[item]) counts[item]++
  else counts[item] = 1;
 
  if (counts[item] > maxCount) {
    maxCount = counts[item];
    maxItem = item;
  }
}

console.log(maxItem);
I feel like there's probably something cleaner using map/reduce. But I was nervous enough on this that I wasn't going to go down that road. If he would have started with the softball oral questions, then got into the code I probably would have been able to think more clear.

I kept trying to figure out a way to combine maxCount and maxItem into one variable (hash or?). But extracting the item value when needed while also being able to compare the counts was ugly. The last line would have been something like console.log(Object.keys(maxHash)[0]). Barf.
What you might do is use indexOf to build an array of unique integers then from that figure out which number occurs most frequently.

Edit : for some reason I read it as max length. I guess you call this the functional way. Isn't that how all the cool kids are doing it nowadays?

Spoiler:

Code:
var arr = [0, 1, 1, 3, 2, 3, 1, 1];
var list = _.reduce(arr, function (memo, ele, index, array) {    
    return array.indexOf(ele) == index ? [].concat.call([], memo, ele) : memo
}, []);
var fin = _.map(list, function(ele) {
    return _.reduce(arr, function(memo, elem, ind) {
        return elem == ele ? [].concat.call([], memo, elem) : memo
    }, []);
});
var sorted = fin.sort(function(a,b) {
  return b.length - a.length
})

return _.first(sorted[0])

Last edited by Craggoo; 04-21-2015 at 03:34 AM.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-21-2015 , 04:22 AM
@Craggoo: That functional version seems to have pretty bad runtime complexity though? I can't imagine array.indexOf having less than O(n) by itself, so this looks very much O(n^2). (But I'm terrible with functional runtimes,...)

Quote:
Originally Posted by kerowo
Can you use the int as an index in another array, adding one to that index as you iterate over the first array? Then find the highest value in the second array. Or hash or dictionary whichever is appropriate?
Yeah, definitely use a hash. Indexing in an array is really bad if your input looks like [1,2,MAX_INT,3]. Esp if those are int64s

And you don't need to iterate over the map, see suzzer's code. Simply keep track of the highest count/value so far.

Quote:
Originally Posted by suzzer99
I kept trying to figure out a way to combine maxCount and maxItem into one variable (hash or?). But extracting the item value when needed while also being able to compare the counts was ugly. The last line would have been something like console.log(Object.keys(maxHash)[0]). Barf.
I like your original version better, but you could get rid of maxCount and replace with the slightly slower:
Code:
  if (counts[item] > counts[maxItem]) {
    maxItem = item;
  }
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-21-2015 , 04:58 AM
I guess i just find endless single if blocks / if - else blocks pretty ugly. The reality is that, if we're going purely for performance, a for loop will always be the fastest.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-21-2015 , 05:05 AM
That algorithm seems to be pretty much optimal if space isn't a issue.

If space was expensive, you could bump it up to O(n log n) by sorting first then going over the sorted array to identify the largest count.

Had a few questions in the past where they'd come up with limitations like that, so just sayin'.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-21-2015 , 05:59 AM
I'd do it like this in C#:

Code:
    var arr = new [] {1, 2, 1, 3, 4, 5, 2, 1, 9,9,9,9};
    var h = new Dictionary<int, int>();
    foreach (var i in arr)
    {
        if (!h.ContainsKey(i)) h.Add(i, 0);
        h[i] ++;
    }
    var result = h.OrderByDescending(c => c.Value).First().Key;
Might be a more efficient way of solving it tbh. Ordering might be a little slow.

Edit, here we are think this is good:

Code:
    var arr = new [] {1, 2, 1, 3, 4, 5, 2, 1, 9,9,9,9};
    var h = new Dictionary<int, int>();
    var mostFreqIndex = 0;
    var mostFreqVal = 0;
    foreach (var i in arr)
    {
        if (!h.ContainsKey(i)) h.Add(i, 0);
        h[i] ++;

        if (h[i] > mostFreqVal)
        {
            mostFreqVal = h[i];
            mostFreqIndex = i;
        }
    }
Edit: just saw that's what was done above me as well, doh!
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-21-2015 , 09:08 AM
Quote:
Originally Posted by suzzer99
There were definitely no questions where I was like "holy cow you guys are on another level".
Remember it was just the phone screen. Nobody wants to ask tough questions when a large percentage of people aren't going to be able to answer them and you'd have to sit through an awkward/painful process.

Last edited by jjshabado; 04-21-2015 at 09:14 AM.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-21-2015 , 09:13 AM
Quote:
Originally Posted by Anais
wasn't sure. been reading project euler and i'm understanding that they frown upon brute forcing answers.
It probably depends on the interviewer, but I like it when someone describes the brute force answer at the start - especially if its reasonable.

My strategy is usually something like:

1. Make sure I understand the question (clarifying questions, diagrams, examples, whatever makes sense)

2. Figure out a simple-to-implement solution (usually brute force or other fairly obvious solution).

3. Point out if its really sub-optimal and spend a minimal amount of time trying to figure out a better solution. You can sometimes just discuss with the interviewer and see if the brute-force solution is reasonable. For example implementing O(N) is possibly ok, implementing O(N^3) is almost never a good idea.

4. Write code.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-21-2015 , 09:28 AM
and all this is done in the span of... 30 minutes? 3 hours?

how long are these tech questions supposed to last?
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-21-2015 , 09:33 AM
30-60 minutes at most.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-21-2015 , 09:40 AM
has anyone read Cracking the Coding Interview? Is it pretty spot on for the types of things you should know for tech interviews? (such as below)

** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-21-2015 , 09:57 AM
Was fiddling around with the Ruby track on codecademy the other day and ran into almost that exact question, except it was counting the frequency of strings. So that had a little more to it with formatting and splitting the words into an array. It's cool to know some of these lessons could be useful, if I remember them later on when I think I'm competent enough to be hireable.

It went a little something like this:

Code:
arr = [0, 1, 1, 3, 2, 3, 1, 1]
frequencies = Hash.new(0)

arr.each { |n| frequencies[n] +=1 }

frequencies = frequencies.sort_by { |k, v| v }
frequencies.reverse!

frequencies.each { |k, v| puts "#{k}: #{v}" }
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-21-2015 , 10:11 AM
Quote:
Originally Posted by Anais
has anyone read Cracking the Coding Interview? Is it pretty spot on for the types of things you should know for tech interviews? (such as below)

This sounds about right in terms of background knowledge but at the end of the day, you need to be able to solve problems on your own.

Edit: also, I don't think "Singleton/Factory" design pattern questions are that common during interviews - I'd replace that with Object-Oriented Design and Inheritance
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-21-2015 , 01:01 PM
Using hashes to find a common element in an array adds additional space complexity.

While an 8 element array isn't a terrible offender of space, consider:
arr = [0, 1, 2, 3, 4, ..., 999999998, 999999999, 999999999, 1000000000]
or something to that effect.

If you aren't allowed to solve it using a hash:
Sort the array.
Track a maximum count, maximum element, current count, and current element.
Start iterating through the array.
All you care about is whether the current element differs from the previous, adjusting current counts, comparing to max count, adjusting maximum element and max count if necessary.

Comparing your max count to the number of elements remaining could prevent iterating through the entire array. If max count is greater than elements remaining, terminate your loop/return max value.

This approach adds additional computational complexity but saves on the space complexity.

I think both approaches have trade-offs and is something worth bringing up in an interview.

Last edited by anfernee; 04-21-2015 at 01:20 PM.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-21-2015 , 01:10 PM
Would anyone mind explaining hash tables to me like I'm 5?
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-21-2015 , 01:49 PM
Quote:
Originally Posted by cannabusto
Would anyone mind explaining hash tables to me like I'm 5?
A hash table is basically an associative array that is optimized for faster look-up times. Say you had an array that mapped people to their phone numbers:

PhoneNumbers["John Smith"] = 555-453-4567
PhoneNumbers["Abigail Adams"] = 555-973-4170
PhoneNumbers["Michael Turner"] = 555-779-0340
...

Imagine there are thousands of entries. If you want the entry for "Michael Turner", you're going to have to search through the keys to find it's index. There could be thousands and thousands of keys, so this might take a while.

What a hash table does, instead, is to use some function to compute the index for each key. This way, when it's time to look up "Michael Turner", you just run the hash function to find his index and then access it directly, without searching through the array. Behind the scenes, your array would look something like this:

PhoneNumbers[d131dd02c5e6eec4] = 555-453-4567
PhoneNumbers[55ad340609f4b302] = 555-973-4170
PhoneNumbers[d8823e3156348f5b] = 555-779-0340

And your hash function would work like this:

SomeHashFunction("Michael Turner") -> returns d8823e3156348f5b

This method also allows for insertions/deletions into the array without worrying about sorting/reordering. For more: http://en.wikipedia.org/wiki/Hash_table
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-21-2015 , 02:01 PM
Really awesome explanation thank you very much.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-22-2015 , 12:00 AM
Huh, look what I just got in my inbox: a variation on Suzzer's question:

https://www.interviewcake.com/questi...e=weekly_email

They first ask for a single duplicate, then ask for finding all duplicates, which I suppose can be then extended to most duplicates. The article is a bit tricky, but an interesting solution.. and what do you know, it uses plain old non-functional programming.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-22-2015 , 01:24 AM
Quote:
Originally Posted by daveT
Huh, look what I just got in my inbox: a variation on Suzzer's question:

https://www.interviewcake.com/questi...e=weekly_email

They first ask for a single duplicate, then ask for finding all duplicates, which I suppose can be then extended to most duplicates. The article is a bit tricky, but an interesting solution.. and what do you know, it uses plain old non-functional programming.
Seems like the non-hash approach I suggested as an alternative would work - with slight modifications of course.
By sorting and then iterating, you don't have to care about the duplicates, only when the elements change.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-22-2015 , 01:36 AM
What do you use to sort? Lodash? I talked about sorting during the code screen but didn't want to mess with it.

Goddamit there is an array.sort() method. I actually mentioned that in the meeting and the dude said there was no native method. I should downgrade him.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-22-2015 , 01:47 AM
Quote:
Originally Posted by suzzer99
What do you use to sort? Lodash? I talked about sorting during the code screen but didn't want to mess with it.

Goddamit there is an array.sort() method. I actually mentioned that in the meeting and the dude said there was no native method. I should downgrade him.
I even included array.sort in my solution
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote

      
m