Open Side Menu Go to the Top

04-22-2015 , 01:54 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.
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
}, []);
return list
That gives you a list of duplicates. From there you can sort it to find most common duplicate or whatever the interviewer wants you to do. Once again, the solution they post uses if-else's.

If we had it inside an actual function ready to test then it would simply be

Code:
function findDuplicates(arr) {
    return _.reduce(arr, function (memo, ele, index, array) {    
        return array.indexOf(ele) != index ? [].concat.call([], memo, ele) : memo
    }, [])
}
Don't think you can get any more compact than that without the use of regex can you?

Furthermore, if you wanted to dazzle the interviewer you could use the findDuplicates function in a recursive function to find the most common duplicate. What fun is it to go a day without recursion?

Last edited by Craggoo; 04-22-2015 at 02:13 AM.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **
$25m Guaranteed WPM on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **
04-22-2015 , 02:40 AM
For algorithm questions like these it's important to understand the implementation and performance of your toolset.

Indexof has a worst case time complexity of n. Worst case you call indexof n times making for an n squared runtime.

Had dinner with a friend who works at Google tonight. He told me about a phone screen he had where he had someone answer an algorithm type question. The guy did ok until he was asked the big o time complexity of the algorithm and said n. My friend said "well the first thing you do here is call sort. Would you care to revise that answer?" But the guy stuck with n time
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-22-2015 , 02:59 AM
According to StackOverflow, array.sort in Javascript uses quicksort for numeric arrays sooooooo I imagine this has better runtime

Code:
function findDuplicates(arr) {
    var sorted = arr.sort(function(a,b) {
        return a - b 
    })
    return _.reduce(sorted, function(memo, ele, index, array) {
        return index == array.length-1 ? memo : array[index+1] == ele ? [].concat.call([], memo, ele) : memo
    }, [])
}
Sort the array first then check that the next element in the array is the same as the current. If you don't like long ternary expressions you can convert it to an if-else if-else statement. Runtime, time complexity, etc is the one thing I do not know anything about.

Last edited by Craggoo; 04-22-2015 at 03:05 AM.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-22-2015 , 03:12 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)

Buying this book is by far the best ROI purchase I have ever made. By far the best interview prep book I have found. Probably best suited for new college hires and for positions below senior. I would imagine interviewing for senior level roles your prep work isn't as focused on these sort of problems.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-22-2015 , 03:29 AM
I know like 80% of those topics in enough detail I could feel comfortable answering interview questions about them.

That is encouraging to me.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-22-2015 , 04:25 AM
Find the number of integers in our input array which lie within the range 1..n/2.

Compare that to the number of possible unique integers in the same range.

If the number of actual integers is greater than the number of possible integers, we know there’s a duplicate in the range 1..n/2, so we iteratively use the same approach on that range.

If the number of actual integers is not greater than the number of possible integers, we know there must be duplicate in the range n/2+1..n, so we iteratively use the same approach on that range.

At some point our range will contain just 1 integer, which will be our answer.


How is this possible without an if statement?

There is no sorting involved....

Of course, this only works if the elements in the array are less than the length of the array. Not sure if suzzer's has that constraint.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-22-2015 , 07:09 AM
Quote:
Originally Posted by daveT
At some point our range will contain just 1 integer, which will be our answer.

How is this possible without an if statement?

There is no sorting involved....

Of course, this only works if the elements in the array are less than the length of the array. Not sure if suzzer's has that constraint.
Seems like this solves a different problem. I think for this algo to work each number in the range must appear in the array at least once & only a single number can have duplicates. Also I think this is nlogn while suzzers solution was already at n but saves on the space for the hash.

Get rid of those annoyingly easy to understand if statements with a nice elegant tangled web of ternary operators.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-22-2015 , 12:23 PM
my bad, didn't see the original post with the link to the question.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-22-2015 , 01:05 PM
got fizzbuzz in javascript on the first attempt
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-22-2015 , 03:26 PM
Does anyone have a learn-by-doing site they liked better than Codecademy? With a similar code in the browser type deal. CC has some "faults" to say the least.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-22-2015 , 03:32 PM
My buddy founded codewars.com
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-22-2015 , 03:44 PM
That's not at all for beginners though heh.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-22-2015 , 04:38 PM
any language in particular?

here's code school's jquery track
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-22-2015 , 04:47 PM
tell your friend his copyright in his footer is 2014
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-22-2015 , 11:30 PM
well, meanwhile in project #trainwreck, I have contributed a ****load to our pointless SRS document. I did 50 use case diagrams, organized them, edited typos from my group lead's incomprehensible functional requirement descriptions. We have 120 pages so far, I've done about half of them, and the only stuff he really likes is the stuff I've made.

So there's 3 of us on the SRS document itself. Me, the group lead, and Carlos who I like but seems a little distracted. No worries there, we are making cool looking (but pointless) diagrams and it'll all turn out okay I think, easily 300 pages worth of material.

The program........... (restaurant POS system) is a disaster. It doesn't even have the bare basic functional reqs up and running, the log in system isn't even partially completed due to some dumb **** doing nothing and just complaining about Java, and the poor girl who has done 85% of the programming so far is overwhelmed.

So, it falls to me to not only finish most of the document but to re-factor the whole damn thing and dust off my year old Java and Swing skills (been doing c++ for a year) and figure out how the hell to get this thing somewhat presentable in a week.

Still gonna get a C in the class. I kind of feel like tanking on purpose since the result might be the same but I feel like I'm getting some valuable life lesson that'll be worth it for me later in my career...at least I keep telling myself that.


**** GROUP PROJECTS. IF YOU HAD JUST MOTHER****ING LET ME PROGRAM IN THE BEGINNING WE WOULD NOT BE IN THIS PREDICAMENT )%()@UI$SIOFJDSIGJSOIKGJLFSGSHG MOTHER****ING SON OF A TURD
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-22-2015 , 11:52 PM
So I was talking about google doc guy earlier and how useless he is, lets call him MS paint guy for now (You'll see why). We were delegating different tasks for each person, and the professor also had some good examples of what things to do in class. It was pretty clear to all of us, what each of us had to do after seeing those examples.

MS Paint guy was delegated to take the task of making sequence diagrams. It's "easy" he said, "I'll do it, I"ll make 20", he said...No problem or so we thought.

He keeps in touch with my other teammate in regards to the project now. So I ask the teammate to ask MS Paint guy how he's doing with the sequence diagram. First MS Paint said he has the files of sequence diagram. Then later he changes his story and said he's drawing them. We are all confused.. but okay whatever, maybe he is just making a draft on hand.

Today is the day we have to meet up and my friend who is in the group, texts me to check the stuff on google drive.

Turns out MS Paint guy is uploading one by one images of sequence diagrams that were made by MS Paint. A total of 13 of these ugly things were uploaded.

Below is one of my favorites. The spelling is exceptional.


Of course professor said it looked like crap and to redo it.

In addition, he even had the nerve to ask what programming language we were using while we were meeting with the professor.

The other guys in group are adamant about to taking over his task, but I think it's not fair to give MS Paint a second chance. So we'll see what he has in store next week.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-22-2015 , 11:54 PM
hahaha so amazing. the best part is the sheer amount of work it must have taken to do such a ****ty job.

give him a box of crayons and see what he can come up with. Select Lunchs, I missed that one. oh man
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-23-2015 , 12:08 AM
You guys better bunker down on it.
I doubt you want to be working on it when exams are rolling around the corner.

I'm sort of amazed at how much documentation you guys are required to do.
My uni just has us write code, comment it up with method descriptions and few comments when something is complex.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-23-2015 , 05:04 AM
You can't spell "phonetically" phonetically without 'fun'!

Last edited by Anais; 04-23-2015 at 05:05 AM. Reason: Or: lol breakfest
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-23-2015 , 08:24 AM
I did all my sequence diagrams in MS Paint. But I just stuck to boxes, lines, and standard text.

Plus it was 10 years ago.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-23-2015 , 09:00 AM
Quote:
Originally Posted by jmakin
well, meanwhile in project #trainwreck, I have contributed a ****load to our pointless SRS document. I did 50 use case diagrams, organized them, edited typos from my group lead's incomprehensible functional requirement descriptions. We have 120 pages so far, I've done about half of them, and the only stuff he really likes is the stuff I've made.

.....

Still gonna get a C in the class. I kind of feel like tanking on purpose since the result might be the same but I feel like I'm getting some valuable life lesson that'll be worth it for me later in my career...at least I keep telling myself that.


**** GROUP PROJECTS. IF YOU HAD JUST MOTHER****ING LET ME PROGRAM IN THE BEGINNING WE WOULD NOT BE IN THIS PREDICAMENT )%()@UI$SIOFJDSIGJSOIKGJLFSGSHG MOTHER****ING SON OF A TURD
You hung in there and got through it, good job. I think it will be valuable. Changing gears from C++ to Java also good experience in my view. The grade doesn't matter very much, what you learned does matter.

Think about this, what if your project had actually taken an approach that was more inline with Agile. Daily scrum meetings, short term sprints, scrum master etc. I am guessing the problems such as those with the sprite approach would have revealed themselves a lot earlier.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-23-2015 , 10:32 AM
Well I guess I muddled through the technical screen ok. On to the next step.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-23-2015 , 11:29 AM
This is for Netflix you say?
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-23-2015 , 01:19 PM
Looking for some algorithm and data structure feedback. My task is to design a reentrant routine that periodically gets run time and must maintain an approximate list of the top X candidates out of a larger ever changing list (order of the big list will always be the same except for new items being added at the end or me removing one of them but scores will change constantly...however scores will only increase)

Is there going to be a more efficient way to do this than maintaining a sorted array of size X, and checking the next item in the bigger list against the lowest score? If it is higher than the lowest score first check to see if it is already in the array and update the score, then place it in the corrrect spot?

Sorry if this is a bit hard to follow typing this on my phone over lunch.

Last edited by KatoKrazy; 04-23-2015 at 01:24 PM.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-23-2015 , 01:50 PM
Quote:
Originally Posted by KatoKrazy
Looking for some algorithm and data structure feedback. My task is to design a reentrant routine that periodically gets run time and must maintain an approximate list of the top X candidates out of a larger ever changing list (order of the big list will always be the same except for new items being added at the end or me removing one of them but scores will change constantly...however scores will only increase)

Is there going to be a more efficient way to do this than maintaining a sorted array of size X, and checking the next item in the bigger list against the lowest score? If it is higher than the lowest score first check to see if it is already in the array and update the score, then place it in the corrrect spot?

Sorry if this is a bit hard to follow typing this on my phone over lunch.
If X is going to be small (as in video game top scores), pretty much anything works. If X is going to grow large, you probably want to use a priority queue where lowest score has the highest priority.

http://en.wikipedia.org/wiki/Priority_queue

Edit: note that if the scores change and you're not notified of the changes, you pretty much have to process the entire list every time. also, you may have to implement locking yourself you're not using concurrent data structures.

Last edited by candybar; 04-23-2015 at 02:06 PM.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **
$25m Guaranteed WPM on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

      
m