Interpretation of Embedded Questions as Relative Clauses
Actually, the sentence itself is stilted and ambiguous in meaning. 

你看了大卫给谁的书。

The way to express your first idea(You read the book that David gave someone):

你看了David给别人的书。

And the second one (You read the book that David gave someone, and who did he give it to?): 

你看的那本书David给谁了？or 你看过的那本书David给谁了？

Both implies that you have read the book; you are questioning who David have given that book to.