A generalized URN problem and its applications