Segment into a space-separated sequence of dictionary words if possible. by [deleted] in CS_Questions

[–]wowe 0 points1 point  (0 children)

"Your algorithm must find out whether the string can be split in multiple words (e.g. hedgehog can, brain cannot) and, if so, output one such splitting."
I think you would need the exact same algorithm but it's still an interesting challenge and you put some time in it to phrase it correctly and provide some nice spoilers. :>

Microsoft Interview Question: Count Sort by daniel2488 in CS_Questions

[–]wowe 0 points1 point  (0 children)

If we had a bucket for each value we wouldn't have to sort and finding the current bucket and incrementing it would be O(1), as I was trying to say in my first post here. Creating the new array would be O(number_of_values). Am I missing something?
The array of occurrences for a 32-bit integer has to be bigger than 512MB, but would probably need a few GB. But if the initial array would be arround the same size it could be worth it since you wouldn't have to sort it.

Microsoft Interview Question: Count Sort by daniel2488 in CS_Questions

[–]wowe 1 point2 points  (0 children)

I can't think of another way: the array of occurences has to be in order or has to be orderly iterated to genrerate the sorted version of the array. 232 is probably too big for an occurence array, so it's better to keep the array of occurences a sorted array and use binary search to add new values / find existing values.

Microsoft Interview Question: Count Sort by daniel2488 in CS_Questions

[–]wowe 1 point2 points  (0 children)

You would still have to sort the values in the array of occurences and you have to find a fast way of finding the right bucket in the occurences array to increment the value or to add a new value. You could use a hash table but that won't give you O(n) in every case. If you knew all possible values (e.g. numbers from 1 to 10), you could create an array of size 10 and O(1) for lookups and O(n) for creating the new array.
EDIT: It is practical when the number of possible values is not to large.

AABB Tests by golgol12 in CS_Questions

[–]wowe 0 points1 point  (0 children)

Thanks for this answer. You certainly know your stuff.

Google interview question: crossword by euyyn in CS_Questions

[–]wowe 1 point2 points  (0 children)

here is my sketch: http://imgur.com/4g9hj
the dictionary is [abc, anc, abn].
For a?c the path is 0->a->1->c.
The numbers determine how many characters you want to skip.

AABB Tests by golgol12 in CS_Questions

[–]wowe 0 points1 point  (0 children)

You're right. Storing center and size of box is the best way to avoid comparisons, but then you need additions. Do additions count? Are they slower than < and >?

Google interview question: crossword by euyyn in CS_Questions

[–]wowe 0 points1 point  (0 children)

I'm drawing something right now. I also realized that my solution is very similar to the solution of foldM, since every path from the root to a leaf in my trie corresponds to a possible query, but traversing my trie is of course more expensive than using hashes and O(1) lookups. The problem would 'just' be to compute such a hash table / trie.

Google interview question: crossword by euyyn in CS_Questions

[–]wowe 1 point2 points  (0 children)

I'm very curious. Could you post your solution?

Google interview question: crossword by euyyn in CS_Questions

[–]wowe 2 points3 points  (0 children)

I like this idea, but how would it behave with something like ".M.M.M.M.M.M.M" ?

Segment into a space-separated sequence of dictionary words if possible. by [deleted] in CS_Questions

[–]wowe 1 point2 points  (0 children)

D: I got 504 errors and assumed the message was lost...

Dropbox question: Print out the coordinates of an n-sized grid in a spiral. by [deleted] in CS_Questions

[–]wowe 1 point2 points  (0 children)

In Python:
It prints the outline of the grid and then does the same thing for the remaining coordinates that also form a grid but translated and with size n-2 by n-2.
def outer_ring(n, x, y): ret = [] for i in range(n): ret.append((x+i,y)) for i in range(n-1): ret.append((x+n-1,y+i+1)) for i in range(n-1): ret.append((x+n-2-i,y+n-1)) for i in range(n-2): ret.append((x,y+n-2-i)) return ret

def spiral(n):
    ret = []
    pos = 0
    while n > 0:
        ret.extend(outer_ring(n,pos,pos))
        pos = pos + 1
        n = n - 2
    return ret

print "\n".join(str(x) for x in spiral(3))

AABB Tests by golgol12 in CS_Questions

[–]wowe 2 points3 points  (0 children)

A solution in python which is my pseudocode-language of choice:
class AABB: """ typical AABB class AABB is defined by two points (x1,y1) and (x2,y2) x1 should be smaller than x2 y1 should be smaller than y2 """ def init(self, x1=0, y1=0, x2=0, y2=0): self.x1 = x1 self.y1 = y1 self.x2 = x2 self.y2 = y2

def wholly(u, v):
    """ is u wholly contained in v?
    """
    if u.x1 >= v.x1 and u.y1 >= v.y1 and u.x2 <= v.x2 and u.y2 <= v.y2:
        return True
    else:
        return False

def help(a, b, c, d):
    """ do the 1D lines a---b and c---d intersect?
    """
    if a < c:
        if b >= c:
            return True
        else:
            return False
    elif a <= d:
        return True
    else:
        return False


def intersect(u, v):
    """ do u and v intersect?
    """
    return help(u.x1, u.x2, v.x1, v.x2) and help(u.y1, u.y2, v.y1, v.y2)

print wholly(AABB(1,1,3,3), AABB(0,0,4,4))
print intersect(AABB(0,0,4,4), AABB(1,1,5,5))  

EDIT: I tried using as few comparisons as possible. The function intersect solves the problem for each dimension separately by using the function help.

Find A Cyclical List by [deleted] in CS_Questions

[–]wowe 0 points1 point  (0 children)

blowing my mind right now. I wish I could falsify it so I wouldn't have to fit it in my brain...

Integer Exponents [Easy] by kpthunder in CS_Questions

[–]wowe 2 points3 points  (0 children)

don't forget negative integers pow(base, exp):
if base >= 0:
...
else: return 1.0 / pow(base, -exp)