puzzle

Best algorithm for this interview problem

Given a NxN matrix with 0s and 1s. Set every row that contains a 0 to all 0s and set every column that contains a 0 to all 0s. For example 1 0 1 1 0 0 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 results in 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 0 A Microsoft Engineer told me that there is a solution that involves no extra mem...

C++ Mystery

Can someone explain to me why this code prints 14? I was just asked by another student and couldn't figure it out. int i = 5; i = ++i + ++i; cout<<i; ...

Checking 2-dimensional array (like eight queens puzzle)

My problem is very similar to eight queens puzzle. I've got 2-dimensional array (N x N) that for example, looks like this: 0,0,0,0,1 y 0,0,0,0,0 | 0,0,0,0,0 V 0,0,0,1,0 0,0,0,0,0 x-> I'm checking horizontally, vertically and diagonally for occurrences of 1 \,0,|,0,/ 0,\,|,/,0 -,-,1,-,- 0,/,|,\,0 /,0,|,0,\ I'm thinking about storin...

How to store sets, to find similar patterns fast?

(This is no homework and no work issue. It's just my personal interest/occupation and completly fictional. But I am interested in a good algorithm or data structure.) Let's assume, that I would run a dating site. And my special feature would be that the singles were matched by movie taste. (Why not?) In that case I would need a way to ...

Extending the parent-child relationship

Given the one-table design given below how would the following best be queried The set of extended family members given a folk id The set of common ancestors given two folk ids The set of descendants given a folk id *Bonus 1st cousins, twice removed given a folk id Table Folk FolkID (PK) MotherID (FK to folkid) FatherID (FK to...

Any recommended websites for SQL puzzles?

Is there any website that contains SQL puzzles, tips/tricks etc? Any version of SQL (Oracle, MSSQL etc) will do. Just want to solve some general SQL puzzles and want to learn more about SQL. Thanks in advance! ...

A way of checking if the digits of num1 are the digits in num2 without checking each digit?

Lets say I have guessed a lottery number of: 1689 And the way the lottery works is, the order of the digits don't matter as long as the digits match up 1:1 with the digits in the actual winning lottery number. So, the number 1689 would be a winning lottery number with: 1896, 1698, 9816, etc.. As long as each digit in your...

What does this mysterious Color Method do? What does it return?

Maybe I've had too much coffee, maybe I've been working too long, regardless, I'm at a loss as to what this method does, or rather, why and how it does it, could anyone shed some light upon me? What is the nextColor? public Color nextColor() { int max = 0, min = 1000000000, cr = 0, cg = 0, cb = 0; for (int r = 0; r < 256; r += 4) ...

What operation turns floating point numbers into a "group" ?

Might anyone be famiiar with tricks and techniques to coerce the set of valid floating point numbers to be a group under a multiplication based operation? That is, given any two floating point numbers ("double a,b"), what sequence of operations, including multiply, will turn this into another valid floating point number? (A valid floa...

Why is my simple comparator broken?

I have a class, which I have simplified to this: final class Thing { private final int value; public Thing(int value) { this.value = value; } public int getValue() { return value; } @Override public String toString() { return Integer.toString(value); } } I want to sort an array of th...

Counting with an Integer Divide-based routine - Is there a formulaic approach?

Consider a routine that counts by successive divide w/ remainder operations. Starting with a 64-bit dividend, the routine divides by a constant divisor. If the remainder is 0, the routine returns. Otherwise, a new dividend is constructed by multiplying the remainder by 2^32 and adding the integer quotient. In code: /// ULong ...

What's the most insidious way to pose this problem?

My best shot so far: A delivery vehicle needs to make a series of deliveries (d1,d2,...dn), and can do so in any order--in other words, all the possible permutations of the set D = {d1,d2,...dn} are valid solutions--but the particular solution needs to be determined before it leaves the base station at one end of the route (imagine t...

Godel, Escher, Bach Typographical Number Theory (TNT) puzzles and solutions

In chapter 8 of Godel, Escher, Bach by Douglas Hofstader, the reader is challenged to translate these 2 statements into TNT: "b is a power of 2" and "b is a power of 10" Are following answers correct?: (Assuming '∃' to mean 'there exists a number'): ∃x:(x.x = b) i.e. "there exists a number 'x' such that x multiplied x equals b" I...

Factorial in C without conditionals, loops and arithmetic operators

How can I find the factorial of a number (from 1 to 10) in C, without using: loop statements like for, while, and do while; conditional operators like if and case; and arithmetic operators like + , − , * , % , /, ++, −−? FYI: I found this question in C aptitude. ...

Programming challenge: can you code a hello world program as a Palindrome?

So the puzzle is to write a hello world program in your language of choice, where the program's source file as a string has to be a palindrome. To be clear, the output has to be exactly "Hello, World". Edit: Well, with comments it seems trivial (not that I thought of it myself of course [sigh].. hat tip to cobbal). So new rule: no...

Patterns of set bits in a byte

Joel mentioned counting the number of set bits in a byte as a programming question in his Guerrilla Guide to Interviewing, and talked of a way to take advantage of patterns that occur in the lookup table. I wrote an article about it awhile back after I found the pattern. To summarize: Number of bits set in a byte in 16x16 0 1 1 2...

Regular expression for finding a regular expression?

Does anyone have code for finding a file that contains a regular expression? I would assume you could have two different flavors, one for BREs and one for EREs. You would think some kind of test suites would have something like an isRegex() test. Can anyone have any code? Looking for something comprehensive of course. I see this was...

Place random non-overlapping rectangles on a panel

I've a panel of size X by Y. I want to place up to N rectangles, sized randomly, upon this panel, but I don't want any of them to overlap. I need to know the X, Y positions for these rectangles. Algorithm, anyone? Edit: All the N rectangles are known at the outset and can be selected in any order. Does that change the procedure? ...

Solution to a recursive problem (code kata)

This came up while talking to a friend and I thought I'd ask here since it's an interesting problem and would like to see other people's solutions. The task is to write a function Brackets(int n) that prints all combinations of well-formed brackets from 1...n. For Brackets(3) the output would be () (()) ()() ((())) (()()) (())() ...

How to find list of possible words from a letter matrix [Boggle Solver]

Lately I have been playing a game on my iPhone called Scramble. Some of you may know this game as Boggle. Essentially, when the game starts you get a matrix of letters like so: F X I E A M L O E W B X A S T U The goal of the game is to find as many words as you can that can be formed by chaining letters together. You can start with an...