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 Today, 02:12 AM   #33851
well named
poorly undertitled
 
well named's Avatar
 
Join Date: Jun 2007
Location: esse est coesse
Posts: 73,996
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

I should be sleeping but since I'm not tired, here's maybe a cuter JS solution, along the same lines, but it returns immediately if it finds the first character has a repeat :P

I'm a little embarrassed at this point how much time I've spent fiddling with this toy problem today. It's kind of relaxing though.

Code:
function findFirstRepeat(test) {
  const seen = {};
  let solution = -1;

  test.split('').some((c, i) => {
    if (!seen.hasOwnProperty(c)) {
      seen[c] = i;
    } else if (solution < 0 || seen[c] < solution) {
      solution = seen[c];
    }
    
    return solution === 0;
  });
  
  return solution >= 0 ? test[solution] : false;
}

console.log(`earliest repeat: ${findFirstRepeat('abadfdasdfasdfasfasf')}`);
well named is offline   Reply With Quote
Old Today, 02:14 AM   #33852
suzzer99
Carpal \'Tunnel
 
suzzer99's Avatar
 
Join Date: Nov 2005
Location: on top of the bell curve
Posts: 85,779
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by well named View Post
This was wrong actually, the order doesn't matter. I must have had some other problem.

Incidentally, this is why I don't like whiteboard problems. I tend to think of some general approach (use a hash to store the first position each character was seen in, keep track of the earliest duplicate...) and then I start writing some code and it doesn't work and I don't quite understand why and then I play with it -- running it a bunch of times -- and then it works. That iterative process works well for me IRL but isn't easy to do at a whiteboard.

Basically I think I have trouble keeping an entire problem in my head, I need the scratch space.
This is exactly what I did. But he ushered me off the stage before I could even finish. It was humiliating. I did email my answer to the original person I talked to later.
suzzer99 is offline   Reply With Quote
Old Today, 02:18 AM   #33853
suzzer99
Carpal \'Tunnel
 
suzzer99's Avatar
 
Join Date: Nov 2005
Location: on top of the bell curve
Posts: 85,779
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by OmgGlutten! View Post
suzzer, i would google cracking the coding interview questions solved with javascript and look that over before your next white board.
I have version 1, and now version 6. Read version 1 a long time ago one and was annoyed. Haven’t cracked version 6 yet.
suzzer99 is offline   Reply With Quote
Old Today, 02:55 AM   #33854
suzzer99
Carpal \'Tunnel
 
suzzer99's Avatar
 
Join Date: Nov 2005
Location: on top of the bell curve
Posts: 85,779
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

On a side note - why does jsfiddle not have a panel for the console.log? It's a big PITA for me on a short-screen Mac to see the code and launch inspector and see the console.log output at the same time. Or am I missing something?

Edit: Well problem solved - I switched to document.write instead.

Last edited by suzzer99; Today at 02:57 AM. Reason: x
suzzer99 is offline   Reply With Quote
Old Today, 03:06 AM   #33855
OmgGlutten!
Pooh-Bah
 
Join Date: Aug 2016
Posts: 3,755
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

i wouldn't read the book. i would find some github reps from people who have worked through them already. then try to glance/read them over.

like this

https://github.com/careercup/CtCI-6t...ion-JavaScript
OmgGlutten! is offline   Reply With Quote
Old Today, 03:07 AM   #33856
jmakin
 
jmakin's Avatar
 
Join Date: Jan 2008
Location: Streaming
Posts: 28,050
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

The single best book Iíve read lately has been ďThe Pragmatic Programmer.Ē

Itís basic concepts we all know, and I did too, but itís written in a very simple and clear way that reinforces things well.
jmakin is online now   Reply With Quote
Old Today, 04:35 AM   #33857
suzzer99
Carpal \'Tunnel
 
suzzer99's Avatar
 
Join Date: Nov 2005
Location: on top of the bell curve
Posts: 85,779
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Ok I think this is about as low as you can get from a big O POV. But code clarity sucks, and who knows how much processing power stuff like split() is taking up.

Code:
function findFirstRepeat(input) {
  const charHash = {};
  const charArray = input.split('');
  const matchArray = new Array(charArray.length);
  
  charArray.forEach((char, index) => {
    if (charHash[char] !== undefined) matchArray[charHash[char]] = 'MATCH';
    else charHash[char] = index;
  });
  
  return charArray[matchArray.indexOf('MATCH')] || 'none found';
}

document.write(findFirstRepeat("abcdebcd") + "<br/>") // should return "b"
document.write(findFirstRepeat("abba")) // should return "a"
I thought about just using the original string instead of splitting it into charArray - and replacing the matched character with * or something. But then if * is in your string you're screwed. So you have to use an array unless you know for sure some characters won't appear in your string.

Last edited by suzzer99; Today at 04:43 AM.
suzzer99 is offline   Reply With Quote
Old Today, 06:43 AM   #33858
maxtower
Carpal \'Tunnel
 
maxtower's Avatar
 
Join Date: Sep 2005
Posts: 7,790
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by Grue View Post
Anyone a principal/staff/architect SE (next step above lead)? What'd you do to get there? Its my medium term goal (~2 years) at my current place.
Easiest path is to work at a rapidly growing company. Likewise advancement in roles is very difficult in a flat or shrinking company. They already have those roles filled and those guys aren't leaving until they retire.
maxtower is offline   Reply With Quote
Old Today, 08:01 AM   #33859
RustyBrooks
Carpal \'Tunnel
 
RustyBrooks's Avatar
 
Join Date: Feb 2006
Location: Austin, TX
Posts: 23,652
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by suzzer99 View Post
Ok I think this is about as low as you can get from a big O POV. But code clarity sucks, and who knows how much processing power stuff like split() is taking up.
Something I think you really need to tighten up is your understanding (or your expression of) big O notation. All the algorithms posted are the same in big O, O(n). From a computer science point of view they're all equivalent, pick the one that you can write correctly on the whiteboard.

Technically an O(n) solution takes
cn + k
where c and k are constants and different solutions will have different values of these. Some might work better for small n or for large n depending on the values of c and k, but all will outperform an O(n^2) or even O(nlogn) once n gets large enough, which is the point of big O.
RustyBrooks 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 11:16 AM.


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