Why is 的 necessary in 你是什么时候买的苹果? I've found the following sentence:

你是什么时候买的苹果？


However, I'm convinced that this sentence should be 你是什么时候买苹果的？.

Why does the sentence above require 的 after 买? What purpose does it function for?
的 is used here for past tense.

你(是)什么时候买苹果的？ -> When did you buy the apple?
你(是)什么时候买苹果？ -> When will you buy the apple?

And 你是什么时候买的苹果？ is same as 你是什么时候买苹果的？, but more oral.