32 ms·
> Each element in Pascal’s Triangle is found by adding the two numbers to the right and left of it on the row above it; where these don’t exist, they are assume
by UnoriginalGuy 14y ago
> Each element in Pascal’s Triangle is found by adding the two numbers to the right and left of it on the row above it; where these don’t exist, they are assumed to be zero.
That gibberish. Unless you know what "Pascal’s Triangle" is prior to seeing this you have no shot of even understanding the exercise.
You haven't even said how you're meant to iterate through the numbers. If this is meant to be an explanation of a "Pascal’s Triangle" it fails at that.
You aren't testing programming, you're testing obscure maths knowledge and the ability to decrypt your question/explanation.
I bet a lot of the other posters had to google this just to answer it in their impressively quick times.
- victorhn 14y agoI remember seeing Pascal's triangle in high school algebra as a way to obtain the coefficients of a polynomial in the form (a+b)^n, i don't see how can one forget the concept, this is far from obscure maths knowledge.
- deservingend 14y agoThere's nothing obscure about this. This is basic secondary school math.
- UnoriginalGuy 14y agoI never studied it or even heard of it. So from my perspective it is obscure. I've never needed this in my programming career up until now. Have you?
- logn 14y agoI agree. "adding the two numbers to the right and left of it on the row above it" makes no sense to me whatsoever. I could interpret that in about many different ways, and during an interview I'd probably crack. To put this in perspective, I got a 1450 on the SATs. My thought process: I take a number, add the right and left on the same row, then put the result on the upper row. Or, add the left and right and current and put it on the row above. After a while I guess you mean any position is the sum of the numbers to the 1 position upper left and upper right.
- thejsjunky 14y ago> I could interpret that in about many different ways If you look at the graphic that always comes with this question, there's only one way you could possibly interpret it. If you somehow misinterpret the text, you would know immediately when you test your mental model against the graphic. To be fair, the screwed up formatting in the OP makes it a little harder to understand; in an interview it would be laid out correctly.
- DanBC 14y ago> My thought process: Would you still think all of those with the diagram? 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1
- malyk 14y agoThis phrase: "...adding the two numbers to the right and left of it on the row above it..." Doesn't make a whole lot of sense. What is the "it"? row 1: is obviously easy. row 2: I think you are adding 0 to 1 and 1 to 0 to get 1 and 1 row 3: You are adding 0 + 1 for the first item, 1 + 1 for the second, and 1 + 0 for the third. row 4: You are adding 0 + 1 for the first number, 1 + 2 for the second...but the directions say "to the left AND right of it" which makes me think that the answer should be 4, not 3. 1 + 2 + 1. Really, you are only adding the number to the left of "it" (which still isn't clear). And actually, for the first half of the line you are adding the number to the left and for the second half of the line you are adding the number to the right. What you want: row 1: 0 + 1 row 2: 0 + 1, 1 + 0 row 3: 0 + 1, 1 + 1, 1 + 0 row 4: 0 + 1, 1 + 2, 2 + 1, 1 + 0 row 5: 0 + 1, 1 + 3, 3 + 3, 3 + 1, 1 + 0 row 6: 0 + 1, 1 + 4, 4 + 6, 6 + 4, 1 + 0 So I think your directions are probably confusing people. Of course, you still probably aren't going to increase your percentages much, but you could be clearer in what the problem actually is.
- DanBC 14y agoSmart people suffer from analysis paralysis - that's why they suck at simple multiple choice questions. "But it could mean [...]" No, just take the obvious. > Each element in Pascal’s Triangle is found by adding the two numbers to the right and left of it on the row above it; where these don’t exist, they are assumed to be zero. Subject of the sentence: 'Each element' So, "adding two numbers to the right and left of it on the row above it" means "adding the two numbers to the above right and above left of an element." > So I think your directions are probably confusing people Not my directions! Still, this thread should be useful to OP. There are people who get the problem, and there are people who don't get the problem, and smartness isn't the deciding factor. (I'm stupid, and not a programmer, but I understood what the problem was and what the desired result would be. Other people are smarter than I am, and are programmers, but didn't seem to grok the question.)
- thejsjunky 14y ago> That gibberish No, it's a fairly straight forward and simple algorithm. The only ambiguity is due to the fact that it references relative spatial positions...but this question always provides a graphic that makes everything completely clear: 1 1 1 1 2 1 1 3 3 1 If someone couldn't understand the question with that description and graphic, they are either having a "brain fart"/interview jitters or have some deficiency in their reading comprehension, spatial reasoning, etc that they need to work on. The former is perfectly understandable and will happen no matter what you do in the interview; the latter tells you that the interviewee needs work in some area which is useful information. > You aren't testing programming, you're testing obscure maths knowledge Not at all, to answer this question requires absolutely no previous knowledge. All you need to answer is in the question. You can pretty much just translate the question directly into code: function getRow(n) { if (n == 1) return [1]; if (n == 2) return [1, 1]; var previous_row = getRow(n - 1); var current_row = [] for (var i = 0; i < n; i++) { current_row.push( (previous_row[i - 1] || 0) + (previous_row[i] || 0) ); } return current_row; } Took me maybe a couple minutes to translate the question into a fairly straightforward recursive function and type it out. From there it would be easy to improve in a lot of ways: caching, making it iterative, etc...all of which are relatively obvious and easy. I doubt there are any interviewers who wouldn't be basically satisfied with the most brain dead implementation. That demonstrates basic competence. The question has ample room to demonstrate a variety of skills beyond that. I'd expect the better interviewees (even with little math skill/background knowledge) to at least be able to make some observations about the properties of the triangle. I'd also expect the better ones to be able to talk about different approaches and their benefits/drawbacks. For candidates with better math skills or background knowledge, they might know offhand/be able to write a better algorithm for computing the nth row. If the interviewer is expecting that as a baseline, sure I agree that's a bit unreasonable...but I don't think that ever happens. I think this is a perfectly fine interview question with the caveats that it's very well known (and hence there will be some with a solution memorized) and one shouldn't rely on it too much. It helps to have a good interviewer who will be able to explain the purpose of the question, and guide the interviewee if they get stuck. This is holds true for any interview question though.
- 14y ago
- thisiswei 14y agodef cal(n): if n == 1: return [1] else: x = cal(n-1) return [1] + [x[i]+x[i-1] for i in range(1,len(x))] + [1] I didnt know "Pascal’s Triangle" , nor did I google, maybe I just got lucky. (No CS degree, self-taught) ---------------------------------------------------------------------------------------------------------------------------- I'm not yet a great programmer, but no doubt I'm becoming one Wanna hire me, for free :) Location: SF,CA ---------------------------------------------------------------------------------------------------------------------------- my site: codefella.herokuapp.com