Cats take a lot of care over their coats and regularly groom themselves by licking off any loose hairs. Cats’ tongues are quite rough and they pull a lot of hair out, most of which they’ll swallow. In small amounts, this just passes straight through the cat’s digestive system. But if, for example, your cat has a particularly thick coat the hair may build up too quickly and form trichobezoar, or fur balls. The quickest way to get these out of the stomach is to vomit them up. As to why your cat chooses the carpet, you’d best ask them!