DanOG99
BAN USER- 0of 0 votes
AnswersGiven an array of five integers that represents a poker hand e.g. [2,2,2,3,3] return the value of the hand, valid values are only "pair","two pairs","three of a kind","full house","four of a kind" we don't have to worry about straight, flush or straight flush.
- DanOG99 in United States
My approach was to have a fixed array of 13 elements (each element represents one possible value of the hand) this array will keep the occurrence of each value of our hand and after we have all the values we just iterate this array and multiply all the values by itself and adding them to get a unique result.
For example if we have [2,2,3,4,5] our fixed array will have a "2" and three "1" so our result will be "2*2+1*1+1*1+1*1",the result will be 7, so all the possible hands will have unique values, if we find a 7 as a result we will know it is a pair, a 9 will be two pairs, a 11 will be three of a kind, 13 will be a full house and 17 will be a four of a kind.
This algorithm is O(1) both in complexity and space but the interviewer didn't like it too much.| Report Duplicate | Flag | PURGE
Microsoft Software Engineer Algorithm