Two Plus Two Publishing LLC
Two Plus Two Publishing LLC
 

Go Back   Two Plus Two Poker Forums > >

Notices

Programming Discussions about computer programming

Reply
 
Thread Tools Display Modes
Old 09-20-2016, 03:31 PM   #76
suited fours
Pooh-Bah
 
suited fours's Avatar
 
Join Date: Nov 2012
Location: East Coast USA
Posts: 4,147
Re: Interview Test Questions Problems, Solutions, Links, Discussion

lol noob help please. I use sublime text and I have it set for a tab to equal 4 spaces. How do I copy my file out such that 2x2 doesn't lose my indenting?
suited fours is offline   Reply With Quote
Old 09-20-2016, 03:33 PM   #77
suzzer99
Save the Cheerleader, Save the World
 
suzzer99's Avatar
 
Join Date: Nov 2005
Location: on top of the bell curve
Posts: 91,970
Re: Interview Test Questions Problems, Solutions, Links, Discussion

Use the code tags [ code ].

Also switch to 2-space tabs. They're objectively better.
suzzer99 is offline   Reply With Quote
Old 09-20-2016, 03:34 PM   #78
toddw8
veteran
 
Join Date: Dec 2003
Posts: 2,472
Re: Interview Test Questions Problems, Solutions, Links, Discussion

Put it inside [ CODE ][ /CODE ] tags (without spaces).
toddw8 is offline   Reply With Quote
Old 09-20-2016, 03:35 PM   #79
suited fours
Pooh-Bah
 
suited fours's Avatar
 
Join Date: Nov 2012
Location: East Coast USA
Posts: 4,147
Re: Interview Test Questions Problems, Solutions, Links, Discussion

Python

Spoiler:
suited fours is offline   Reply With Quote
Old 09-20-2016, 03:39 PM   #80
candybar
old hand
 
Join Date: Aug 2011
Posts: 1,955
Re: Interview Test Questions Problems, Solutions, Links, Discussion

wanted to try something different:
Spoiler:
candybar is offline   Reply With Quote
Old 09-20-2016, 03:43 PM   #81
suited fours
Pooh-Bah
 
suited fours's Avatar
 
Join Date: Nov 2012
Location: East Coast USA
Posts: 4,147
Re: Interview Test Questions Problems, Solutions, Links, Discussion

Quote:
Originally Posted by suzzer99 View Post
Use the code tags [ code ].

Also switch to 2-space tabs. They're objectively better.
Appreciate the feedback. "Python Crash Course" said PEP8 is the style guide to follow, which says 4 spaces. Thoughts?
suited fours is offline   Reply With Quote
Old 09-20-2016, 03:45 PM   #82
suzzer99
Save the Cheerleader, Save the World
 
suzzer99's Avatar
 
Join Date: Nov 2005
Location: on top of the bell curve
Posts: 91,970
Re: Interview Test Questions Problems, Solutions, Links, Discussion

4 spaces suck. That is my thoughts. Why would you want to waste horizontal space?
suzzer99 is offline   Reply With Quote
Old 09-20-2016, 04:40 PM   #83
noname6520
veteran
 
Join Date: Feb 2008
Posts: 3,409
Re: Interview Test Questions Problems, Solutions, Links, Discussion

Another JS solution:

Spoiler:
noname6520 is offline   Reply With Quote
Old 09-20-2016, 04:49 PM   #84
noname6520
veteran
 
Join Date: Feb 2008
Posts: 3,409
Re: Interview Test Questions Problems, Solutions, Links, Discussion

Quote:
Originally Posted by suzzer99 View Post
4 spaces suck. That is my thoughts. Why would you want to waste horizontal space?
Because 2 spaces make the code look cluttered and harder to read. I never end up nesting my code that much anyway so the horizontal space isn't a big issue.
noname6520 is offline   Reply With Quote
Old 09-20-2016, 05:02 PM   #85
Prickly Pear
enthusiast
 
Prickly Pear's Avatar
 
Join Date: Aug 2016
Posts: 60
Re: Interview Test Questions Problems, Solutions, Links, Discussion

my initial instinct was already done so I tried another approach in js,
Spoiler:
Prickly Pear is offline   Reply With Quote
Old 09-20-2016, 08:51 PM   #86
bex989
centurion
 
bex989's Avatar
 
Join Date: Nov 2013
Posts: 149
Re: Interview Test Questions Problems, Solutions, Links, Discussion

Code:
static double[][] properMatrix = {
    {1, 2, 3, 4},
    {5, 6, 7, 8},
    {9, 10, 11, 12},
    {13, 14, 15, 16},
    {17, 18, 19, 20}
};

private static String fun(double[][] in) {
    if (java.util.Arrays.equals(in, properMatrix)) {
        return "1 2 3 4 8 12 16 20 19 18 17 13 9 5 6 7 11 15 14 10";
    } else {
        return "Undefined.";
    }
}

Last edited by bex989; 09-20-2016 at 09:09 PM.
bex989 is offline   Reply With Quote
Old 09-20-2016, 08:54 PM   #87
suzzer99
Save the Cheerleader, Save the World
 
suzzer99's Avatar
 
Join Date: Nov 2005
Location: on top of the bell curve
Posts: 91,970
Re: Interview Test Questions Problems, Solutions, Links, Discussion

Meets the requirements as stated. Although does array1.equals(array2) actually work in Java? It's been a few years but I don't remember anything like that.

In JS === would test that the references are pointing to the same instance. Which isn't really what you want.
suzzer99 is offline   Reply With Quote
Old 09-20-2016, 09:06 PM   #88
Lattimer
I are smart
 
Lattimer's Avatar
 
Join Date: Feb 2009
Location: New England
Posts: 14,012
Re: Interview Test Questions Problems, Solutions, Links, Discussion

haha well done
Lattimer is offline   Reply With Quote
Old 09-20-2016, 09:12 PM   #89
bex989
centurion
 
bex989's Avatar
 
Join Date: Nov 2013
Posts: 149
Re: Interview Test Questions Problems, Solutions, Links, Discussion

Quote:
Originally Posted by suzzer99 View Post
Meets the requirements as stated. Although does array1.equals(array2) actually work in Java? It's been a few years but I don't remember anything like that.

In JS === would test that the references are pointing to the same instance. Which isn't really what you want.
Yeah, wouldn't work with Object's equals, have to use Arrays.equals. Fixed.
bex989 is offline   Reply With Quote
Old 09-20-2016, 11:10 PM   #90
daveT
S.A.G.E. Master
 
daveT's Avatar
 
Join Date: Jun 2005
Location: La La Land
Posts: 23,148
Re: Interview Test Questions Problems, Solutions, Links, Discussion

Quote:
Originally Posted by suzzer99 View Post
Here's my solution in JS:

Spoiler:
I don't know what the rules were for Googling either, but in general, I don't bother doing anything that I can't do w/o using Google. I know enough of the mechanics of the languages I use that I can find some solution to whatever I'm trying. May not be elegant, but it'll get there.

But I love this solution. I didn't think of trying out a rotate. Interesting approach.

Quote:
Originally Posted by candybar View Post
wanted to try something different:
Spoiler:
This is like trying to figure out how Hendrix plays the Star Spangled Banner. Why bother? Just appreciate.

If I'm interpreting this code correctly, I forgot to add one thing: this had to work on any MxN matrix.

Quote:
Originally Posted by suited fours View Post
Appreciate the feedback. "Python Crash Course" said PEP8 is the style guide to follow, which says 4 spaces. Thoughts?
Quote:
Originally Posted by suzzer99 View Post
4 spaces suck. That is my thoughts. Why would you want to waste horizontal space?
You need to follow PEP8. Python is incredibly concise and I don't think I've ever once ran past 80 chars (which is PEP8 as well).

Other things that may irritate you: Always use spaces, not tabs.

White space is critical to Python, so following stuff that is universal allows anyone to work on anyone else's code.

Quote:
Originally Posted by bex989 View Post
Code:
static double[][] properMatrix = {
    {1, 2, 3, 4},
    {5, 6, 7, 8},
    {9, 10, 11, 12},
    {13, 14, 15, 16},
    {17, 18, 19, 20}
};

private static String fun(double[][] in) {
    if (java.util.Arrays.equals(in, properMatrix)) {
        return "1 2 3 4 8 12 16 20 19 18 17 13 9 5 6 7 11 15 14 10";
    } else {
        return "Undefined.";
    }
}
Why not flatten the array then do random permutations until it matches the string you want? When asked about the worst case, just argue that you will almost never hit n! so it doesn't really matter.

Here is mine:

Spoiler:
daveT is offline   Reply With Quote
Old 09-21-2016, 12:12 AM   #91
candybar
old hand
 
Join Date: Aug 2011
Posts: 1,955
Re: Interview Test Questions Problems, Solutions, Links, Discussion

Quote:
Originally Posted by daveT View Post
If I'm interpreting this code correctly, I forgot to add one thing: this had to work on any MxN matrix.
I think mine does though I haven't tested it. There's an unused variable though lol. It's just the same as the simple solutions posted earlier, with hard-coded conditionals swapped out for data. I like some of the rotation solutions here but for extra credit, I want to see logical, as opposed to physical rotation (with optimization for multiple rotations if you know what i mean). Maybe I'll do a VirtualGridFactory solution
candybar is offline   Reply With Quote
Old 09-21-2016, 01:53 AM   #92
suzzer99
Save the Cheerleader, Save the World
 
suzzer99's Avatar
 
Join Date: Nov 2005
Location: on top of the bell curve
Posts: 91,970
Re: Interview Test Questions Problems, Solutions, Links, Discussion

Here's a version that doesn't rotate the matrix but instead munches around the outside like Pacman.

Code:
let matrix = [
[1,  2,  3,  4],
[5,  6,  7,  8],
[9, 10, 11, 12],
[13, 14, 15, 16],
[17, 18, 19, 20]];

const output = [];
let backwards = false;

var doPush = val => {
  output.push(val);
};

var rowMuncher = (matrix, backwards) => {
  if (backwards) 
    matrix.pop().reverse().map(doPush); 
  else 
    matrix.shift().map(doPush); 
};

var colMuncher = (matrix, backwards) => {
  if (backwards) {
    const len = matrix.length;
    for (let i=len-1; i>=0; i--) {
      doPush(matrix[i].shift());
    }
  }
  else {
    matrix.map(row => { doPush(row.pop()); }); 
  }
};

while (matrix.length) {
  rowMuncher(matrix, backwards);
  colMuncher(matrix, backwards);
  backwards = !backwards;
}

console.log(output);
I realized pop() and shfit() return the exact thing I want to extract output from. Probably the first time I've ever used them for dual purpose like that - remove the thing from the array, and still use the thing you removed. Usually I just throw away the removed thing.

If the reverse() in the rowMuncher is a performance problem you could always swap out with an ugly reverse for loop like I had to do in the colMuncher. It was either that or I had to reverse my matrix, then reverse it back when I was done - which seemed messier and heavier.

Last edited by suzzer99; 09-21-2016 at 02:04 AM.
suzzer99 is offline   Reply With Quote
Old 09-21-2016, 08:33 AM   #93
jjshabado
Carpal Tunnel
 
jjshabado's Avatar
 
Join Date: Jul 2006
Posts: 22,543
Re: Interview Test Questions Problems, Solutions, Links, Discussion

Same idea as a bunch of people here (but of course I like mine best!):

Code:
def getSwirl(m):
    return getLoop(m, 0)
    
def getLoop(m, depth):
    leftBound = depth
    topBound = depth
    rightBound = len(m[0]) - 1 - depth
    bottomBound = len(m) -1 - depth
    
    if leftBound >= rightBound or topBound >= bottomBound:
        return []
    
    result = []
    for index in range(leftBound, rightBound + 1):
        result.append(m[topBound][index])
    
    for index in range(topBound + 1, bottomBound + 1):
        result.append(m[index][rightBound])
        
    for index in range(rightBound - 1, leftBound - 1, -1):
        result.append(m[bottomBound][index])
    
    for index in range(bottomBound - 1, topBound, -1):
        result.append(m[index][leftBound])
        
    result += getLoop(m, depth+1)
    return result

    
    
M1 = [ [1,2,3,4],
       [5,6,7,8],
       [9,10,11,12],
       [13,14,15,16],
       [17,18,19,20] ]

M2 = [ [1,2],
       [3,4],
       [4,5],
       [6,7],
       [8,9],
       [10,11]]
    
    
print getSwirl(M1)

print getSwirl(M2)
Edit: Just to add, I think destroying the input matrix is generally bad. Although I would hope an interviewer wouldn't ding you for it - especially if you mentioned it as a consequence of your solution.
jjshabado is offline   Reply With Quote
Old 09-21-2016, 09:12 AM   #94
jjshabado
Carpal Tunnel
 
jjshabado's Avatar
 
Join Date: Jul 2006
Posts: 22,543
Re: Interview Test Questions Problems, Solutions, Links, Discussion

Gah, just realized I have a bug in the base case check. Oh well, I guess I don't get the job.
jjshabado is offline   Reply With Quote
Old 09-21-2016, 10:43 AM   #95
Sholar
Carpal \'Tunnel
 
Sholar's Avatar
 
Join Date: Jul 2007
Posts: 6,358
Re: Interview Test Questions Problems, Solutions, Links, Discussion

Fun question! Maybe a little tricky for a whiteboard interview. Feels like recursion is always the right answer in an interview setting--same solution technique here as others have already used.

Spoiler:

Posting only because a little python array magic makes this a one-liner...
Spoiler:
Sholar is offline   Reply With Quote
Old 09-21-2016, 12:24 PM   #96
Lattimer
I are smart
 
Lattimer's Avatar
 
Join Date: Feb 2009
Location: New England
Posts: 14,012
Re: Interview Test Questions Problems, Solutions, Links, Discussion

I just realized my while condition should be OR not AND. The rotate idea is great but not practical for a low-level language like C, and unfortunately like 95% of my programming is done in C
Lattimer is offline   Reply With Quote
Old 09-21-2016, 02:04 PM   #97
suzzer99
Save the Cheerleader, Save the World
 
suzzer99's Avatar
 
Join Date: Nov 2005
Location: on top of the bell curve
Posts: 91,970
Re: Interview Test Questions Problems, Solutions, Links, Discussion

Quote:
Originally Posted by jjshabado View Post
Edit: Just to add, I think destroying the input matrix is generally bad. Although I would hope an interviewer wouldn't ding you for it - especially if you mentioned it as a consequence of your solution.
Yeah I went back and forth on that. It would all depend on the application of course.
suzzer99 is offline   Reply With Quote
Old 09-21-2016, 03:24 PM   #98
Rampage_Jackson2
centurion
 
Rampage_Jackson2's Avatar
 
Join Date: May 2012
Posts: 124
Re: Interview Test Questions Problems, Solutions, Links, Discussion

A lot of good interview questions have the form where there is an obvious naive solution, a better solution using some data structure, and then an optimal solution.

If you know the naive solution it's best to state that right away, and say you know there's something better. I think a lot of people try looking for an optimal solution right away, and then get stuck. At the end it looks like they didn't understand at all.
Rampage_Jackson2 is offline   Reply With Quote
Old 09-21-2016, 04:10 PM   #99
suzzer99
Save the Cheerleader, Save the World
 
suzzer99's Avatar
 
Join Date: Nov 2005
Location: on top of the bell curve
Posts: 91,970
Re: Interview Test Questions Problems, Solutions, Links, Discussion

Yeah my Netflix take home assignment was basically "design your own 2-way data-binding framework from scratch" - which you would never do in the real world. Nor was the level of detail they wanted really communicated.

IE - I got dinged for not creating some kind of shadow dom and instead relying on dom state to determine future behavior. I even called out in my comments that is probably something I would flesh out more if I really had to do this. I don't think the person looked at my assignment even read my comments.

I ran it by candybar and another bonafide JS expert I know. They both thought the project was poorly worded and weirdly graded.
suzzer99 is offline   Reply With Quote
Old 09-21-2016, 07:09 PM   #100
daveT
S.A.G.E. Master
 
daveT's Avatar
 
Join Date: Jun 2005
Location: La La Land
Posts: 23,148
Re: Interview Test Questions Problems, Solutions, Links, Discussion

Quote:
Originally Posted by Rampage_Jackson2 View Post
A lot of good interview questions have the form where there is an obvious naive solution, a better solution using some data structure, and then an optimal solution.

If you know the naive solution it's best to state that right away, and say you know there's something better. I think a lot of people try looking for an optimal solution right away, and then get stuck. At the end it looks like they didn't understand at all.
The matrix is the data structure, which can be used as the base for other data structures, like a graph, but not sure how using the matrix in another form would be helpful. Clearly converting this over to a hash or tree is terrible.

I also find it interesting that pretty much every solution here (including mine after thinking on it) has a bug in it. I guess I shouldn't kick myself too hard over bombing this one under interview pressure and knowing I only had about 15 minutes to do it. If accuracy is paramount, I'd probably need a good hour of design and unit testing.

I talked to one of my roommates about it, and he believes that a good interview question is one that you are meant to mess up, where the point is to simply see how you approach solving the problem (the interviewer told me he cared about my approach more than my ability to do it). The tests we gave at a prior job was very much like this. No one ever "passed" the battery of tests we gave, but that was by design. What we were actually looking for had nothing at all to do with the test, in a strange way.
daveT is offline   Reply With Quote

Reply
      

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Forum Jump


All times are GMT -4. The time now is 10:11 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
Copyright 2008-2017, Two Plus Two Interactive
 
 
Poker Players - Streaming Live Online