Open Side Menu Go to the Top
Register
Expected Interview Knowledge Expected Interview Knowledge

09-24-2016 , 02:12 PM
Quote:
Originally Posted by Barrin6
here's my solution, too lazy to write code, will do it in pseudocode :P
I'm not bagging on your solution, but if anyone ever asks exactly what is meant by "modern languages are becoming more expressive" I'd answer with your solution and this (python):

Spoiler:

Code:
def fun(input):
    d = defaultdict(int)
    for word in input.split():
        d[word] += 1

    return sorted(d.keys(), key=lambda x: d[x])[-1]


My solution is absolutely not the most efficient way to do it. I mean, it's O(n) like any reasonable solution, but there are probably faster ways to get there.

But: I think anyone who knows even a little python can:
1. understand what is happening ine function in just a few minutes
2. convince themselves of it's correctness without much effort
and that's what we mean when we talk about "cognitive load" in programming
Expected Interview Knowledge Quote
09-24-2016 , 02:57 PM
Once you're using defaultdict, can just skip ahead to using the builtin most_common for this via Counter
Expected Interview Knowledge Quote
09-24-2016 , 03:07 PM
Even better. I should familiarize myself more with what's in collections.
Expected Interview Knowledge Quote
09-24-2016 , 03:12 PM
Quote:
Originally Posted by Craggoo
You're right my definition sucks. Will you get mad if i start using poor grammar too?
I don't care about poor grammar but there's a difference between something that's merely poorly worded and something that means something completely different from what you intended. It's like a difference between a program that won't compile because you're missing a semicolon and a program that subtly does something different from what you're expecting it to do.
Expected Interview Knowledge Quote
09-24-2016 , 05:35 PM
Quote:
Originally Posted by RustyBrooks
I mean, it's O(n) like any reasonable solution, but there are probably faster ways to get there.
I think you can just keep track of what the max occurring word is as you populate the hash instead of sorting/searching for the max afterward. Though you may want to make sure that extra baggage isn't outweighing the benefit. A word list like 'a aa aa aaa aaa aaa aaaa aaaa aaaa aaaa...' may throw a wrench into it.
Expected Interview Knowledge Quote
09-24-2016 , 05:39 PM
Right, but my point was actually that this would probably perform fairly well, while being expressed as simply as I could think of (obviously, Sholar's correction makes it even simpler)

In anything that's not a programming contest or a job interview, I'm probably going to pick a method that performs "ok" over one that is the top performer but takes more thought to process.
Expected Interview Knowledge Quote
09-24-2016 , 06:09 PM
Well, the solution Rusty gave is O(N log N) in the worst case because of the sort--a trivial fix is to just use max() rather than sorted()[-1] to get the (optimal) O(N) runtime.

Using all of the machinery in collections gets at this with, e.g.
Spoiler:
Counter(input_string.split()).most_common(1)


Getting better performance than that requires at least one good idea :-)
Expected Interview Knowledge Quote
09-25-2016 , 02:59 AM
Quote:
Originally Posted by RustyBrooks
I'm not bagging on your solution, but if anyone ever asks exactly what is meant by "modern languages are becoming more expressive" I'd answer with your solution and this (python):

Spoiler:

Code:
def fun(input):
    d = defaultdict(int)
    for word in input.split():
        d[word] += 1

    return sorted(d.keys(), key=lambda x: d[x])[-1]


My solution is absolutely not the most efficient way to do it. I mean, it's O(n) like any reasonable solution, but there are probably faster ways to get there.

But: I think anyone who knows even a little python can:
1. understand what is happening ine function in just a few minutes
2. convince themselves of it's correctness without much effort
and that's what we mean when we talk about "cognitive load" in programming
Very interesting solution. This is why I am afraid of using python in an interview. I just do not know enough about to use all these fancy tricks the language has. I will keep practicing with it though.
Expected Interview Knowledge Quote
09-25-2016 , 12:10 PM
I happen to think it's a pretty good "interview" language but in particular, I'd disagree with that reason to avoid it...Rusty's solution could be stripped of all the fancy stuff and only add a couple of lines of code and barely hurt readability. (And I would guess if you gave the solution I posted, they might ask you to answer without using any collections magic anyway.)
Expected Interview Knowledge Quote
09-25-2016 , 03:01 PM
defaultdict is just laziness, it's not particularly magic. It just lets me do
x[foo] += 1
instead of
if foo not in x: x[foo] = 0
x[foo] += 1

It might not seem like much but it's so much better if you can hold an entire function in your head, and every if statement makes that tougher.

I think you could do it about as short using map/reduce but I actually don't really like python's syntax much in that regard. I wish you could do
iterablething.map(fun)
like in JS instead of
map(fun, iterablething)

Also it kind of annoys me that lambdas have to be expressions - for example you can not use map to populate the dict, i.e. you can't do
map(lambda x: d[x] +=1, words)
because d[x] += 1 is NOT a python expression.

(I understand that these things are both intentional choices in the language design, but they're occaisonally annoying)
Expected Interview Knowledge Quote
09-26-2016 , 08:40 AM
Quote:
Originally Posted by Barrin6
Very interesting solution. This is why I am afraid of using python in an interview. I just do not know enough about to use all these fancy tricks the language has. I will keep practicing with it though.
Python is a surprisingly huge language, but it is very simple to get the point across without using all the goodies in the Standard Lib. Unless you are applying to a job where being a Pythonista is requirement, using it for an interview to be quick and dirty is okay, IME. I only recall being nicked once or twice for not knowing stuff about Python, but these were, aferall, for Python jobs.

If anything, I've read more "requirements" that requested not using Java than any other language. I find that strange, but I digress.

Collections is an awesome library, btw. I flipping hate using lambda and flat out refuse to use it.
Expected Interview Knowledge Quote
09-26-2016 , 10:21 AM
Quote:
Originally Posted by daveT
I flipping hate using lambda and flat out refuse to use it.
This is cutting off your nose to spite your face, imo
Expected Interview Knowledge Quote
09-27-2016 , 04:29 PM
Had to do something similar for homework where I counted letters in a given string , so I adapted that to work for "words". It's not pretty, but I'm fairly sure it works. Anyone have test data?

Ruby:
Spoiler:

Code:
def most_common_word(input_string)
  input_string.split(" ").inject(Hash.new(0)) do |h, word|
    h[word.downcase] +=1; h
  end.sort_by { |k, v| v }.reverse.first
end

Last edited by fredd-bird; 09-27-2016 at 04:35 PM.
Expected Interview Knowledge Quote
09-30-2016 , 11:18 PM
Quote:
Originally Posted by RustyBrooks
defaultdict is just laziness, it's not particularly magic. It just lets me do
x[foo] += 1
instead of
if foo not in x: x[foo] = 0
x[foo] += 1

It might not seem like much but it's so much better if you can hold an entire function in your head, and every if statement makes that tougher.

I think you could do it about as short using map/reduce but I actually don't really like python's syntax much in that regard. I wish you could do
iterablething.map(fun)
like in JS instead of
map(fun, iterablething)

Also it kind of annoys me that lambdas have to be expressions - for example you can not use map to populate the dict, i.e. you can't do
map(lambda x: d[x] +=1, words)
because d[x] += 1 is NOT a python expression.

(I understand that these things are both intentional choices in the language design, but they're occaisonally annoying)
Maybe a module that uses a partially applied version of the built-in methods would be a sweet spot. So something like s_map (short for standard map) which accepts a list of arguments like you're used to in js.

While lambdas are cool in Python, the 'no statements in lambdas' in Python also makes you go alsdkjf;lasdkjfls;adjfsl;dajkflsadjfslaj;df. You are left with 2 choices basically :

1) Only use lambdas for exceedingly simple logic which kind of defeats how awesome they are

2) Stay away from lambda entirely

Quote:
Originally Posted by daveT
Python is a surprisingly huge language, but it is very simple to get the point across without using all the goodies in the Standard Lib. Unless you are applying to a job where being a Pythonista is requirement, using it for an interview to be quick and dirty is okay, IME. I only recall being nicked once or twice for not knowing stuff about Python, but these were, aferall, for Python jobs.

If anything, I've read more "requirements" that requested not using Java than any other language. I find that strange, but I digress.

Collections is an awesome library, btw. I flipping hate using lambda and flat out refuse to use it.
The one thing that I've found rather annoying while tinkering around in Python is how many import statements you need just to have access to what most would thinks is *very* basic functionality. You have to import itertools for christs sake!
Expected Interview Knowledge Quote
10-01-2016 , 01:17 AM
Quote:
Originally Posted by Craggoo
The one thing that I've found rather annoying while tinkering around in Python is how many import statements you need just to have access to what most would thinks is *very* basic functionality. You have to import itertools for christs sake!
Wait, are you comparing it to, like, javascript? Which literally comes with nothing and everyone's JS project downloads 400 node modules?

(fwiw I almost never use itertools, and don't consider it like a "basic" tool that I'd use a lot. Also, imports in python are basically on par with any other language I've used, like C/C++/Java/etc)
Expected Interview Knowledge Quote
10-01-2016 , 01:42 AM
Quote:
Originally Posted by RustyBrooks
Wait, are you comparing it to, like, javascript? Which literally comes with nothing and everyone's JS project downloads 400 node modules?

(fwiw I almost never use itertools, and don't consider it like a "basic" tool that I'd use a lot. Also, imports in python are basically on par with any other language I've used, like C/C++/Java/etc)
From what i've seen, you can only do very basic for loops in Python without the use iof imports (in my example, itertools). The "400 node modules" you are referring to makes it easier to work in js. The "400 node modules" do not "create" functionality that doesn't exist if that makes sense?

For example... underscorejs has a _.isEqual function (to compare two objects). There is no built-in method to do that in JS. This does not extend js built-ins but complements it. I *NEED* something like itertools if i want to iterate over something in Python.
Expected Interview Knowledge Quote
10-01-2016 , 01:56 AM
Can you give an example of a for loop you can't do without itertools? I literally use it like once a year, tops
Expected Interview Knowledge Quote
10-01-2016 , 04:29 AM
The Python documentation shows the equivalent function for each item in itertools:

https://docs.python.org/3/library/itertools.html
Expected Interview Knowledge Quote
10-01-2016 , 09:33 AM
Quote:
Originally Posted by daveT
The Python documentation shows the equivalent function for each item in itertools:

https://docs.python.org/3/library/itertools.html
Yeah but most people would rather import than write a function.

I mean, there is some useful stuff in there, although much of it can be done fine through list comprehensions or other ways.

I'm just curious what part of it is so essential that he says he can't do more than very basic loops without itertools?
Expected Interview Knowledge Quote
10-01-2016 , 08:41 PM
Python2 has a few things where you need itertools to get the lazy version--this is less true in Python3. Otherwise, most imports from the standard library add (rather than improve) functionality and it feels pretty standard/low effort.
Expected Interview Knowledge Quote
10-01-2016 , 08:50 PM
I looked over itertools again today to see if there was anything I was missing. Most of it is, like you say, lazy versions (iterators vs explicit). This might matter for large lists or cases where the size of the data is unknown, such as with a database, but largely I find it to not be a problem. I used to spend a lot of time making sure my functions returned iterators instead of lists, but I sort of saw the light when I looked at actual performance numbers and saw that in practical cases there was not much benefit.
Expected Interview Knowledge Quote
10-08-2016 , 03:29 AM
A cool problem I just came up with...

given the following where x represents the amount of numbers and y represents the maximum number, return a list that has a length of x where every number is less than y. You can assume for this problem that x will never be greater than y:

Code:
function(x,y) {
    return list
}
For example...

function(10, 10) would return [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

That would be the only valid output give those parameters in this problem.
Expected Interview Knowledge Quote
10-08-2016 , 10:32 AM
I don't think I understand. What is function(5, 10) supposed to return?
Expected Interview Knowledge Quote
10-08-2016 , 02:07 PM
Code:
def f(x, y):
    return [y - 1] * x
If y is never negative:

Code:
def f(x, y):
    return [0] * x
Expected Interview Knowledge Quote
10-08-2016 , 02:11 PM
I think his question is underspecified - your answer can't be right though because he says that f(10, 10) only has one answer, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

I can't think of any interpretation of the question that makes it a good or interesting interview question. It can always be answered by returning the numbers from 0 to x-1.
Expected Interview Knowledge Quote

      
m