Both sentences are grammatical. You have discovered something that often surprises learners from non-Germanic languages: in English, prepositions often follow the verb and are often heard as part of the verb. In a question, the object is often moved to the beginning of the sentence, leaving the verb at the end of the sentence. It's often natural to keep the preposition with the verb, at the end of the sentence.
English grammar often works by analogies with familiar patterns rather than by simple rules. So, instead of a rule to memorize, here are some ordinary sentences that illustrate the kinds of analogies that play a role in your question:
I'm reading The Hobbit.
Which book are you reading?
"Read" is a transitive verb (it takes an object with no preposition), so when you convert the statement to a question by putting the object at the beginning of the sentence, "reading" goes the end. This is the simplest pattern. The others are variations that address additional complexity.
I will be sleeping in on Saturday.
What day will you be sleeping in?
"Sleep in" is a phrasal verb: the verb actually consists of both words. It means to stay asleep in bed beyond the time when you usually wake up in the morning: for example, sleeping until 10:00 a.m. when normally you wake up at 6:30 a.m. It would be ungrammatical, in the question form of the sentence, to put "in" anywhere but after "sleeping". The preposition "in" doesn't introduce an object; its only function in these sentences is to help form the phrasal verb "sleep in".
I'm sitting on the green chair.
Which chair are you sitting on?
On which chair are you sitting?
"Sit" is an intransitive verb: it needs "on" to introduce what you're sitting on.* "Sit on" is not a phrasal verb. But the pattern is similar enough to the previous one that we usually keep "sitting" together with "on" at the end of the sentence. Because "on" introduces an object (unlike "in" in "sleeping in"), it's also grammatical to move "on" to the beginning of the question, where it introduces its object (the chair).
The preposition controversy
By the way, there has long been a controversy about whether it's grammatically incorrect to end a sentence with a preposition in English. Some argue that a preposition introduces an object, so the object must follow it, and therefore the preposition can't end the sentence. I think that's a false analogy with Latin grammar: in English, prepositions often follow verbs without introducing an object. In 1762, Robert Lowth observed that the putting the preposition at the start of the phrase sounds more dignified and formal. In later years, many schoolteachers converted Lowth's observation into a "rule" that it's ungrammatical to end a sentence with a preposition, but English has never worked that way. Lowth's observation is still valid, though. The form with the preposition at the start of the sentence sounds more formal—probably too formal for most casual conversation.
*As often happens in English, there are also transitive senses of "sit", as in "sit an exam". But "sit" in the sense of being seated on a chair does not take a direct object.