The occult whodunit is not an extensive genre, and Stephen King’s “The Outsider” is a good example of why.

The first half is a fascinating mystery that challenges fans of detective yarns to be on top of their game. But the second half veers into the supernatural, with the whodunit becoming a whatdunit, and we’re subjected to a monstrous — literally — deus ex machina.

King’s problem is trying to satisfy two audiences with contradictory expectations. Horror fans want to be shocked; mystery buffs want to deduce the outcome before the author tells us. But just as we’re trying to put together the clues to see if we can solve the case, King pulls a bait and switch.

Then again, it’s King, so why are we surprised by the arrival of a monster or two? Granted, he’s our resident horrormeister, but he has successfully ventured into other genres, including science-fiction (“The Running Man”), prison-escape drama (“Rita Hayworth and Shawshank Redemption”) and nonfiction (“Faithful,” in which the ardent Boston Red Sox fan chronicled the team’s World Series-winning 2004 season). He knows how to analyze the structure and content of various literary forms and then deliver on the template.

And for detective fiction fans, he does a great job of it for the first 250 pages of this book. The plot is built on a fascinating premise: After a brutal murder, both the prosecution and the defense teams have irrefutable proof of their case. What neither side can figure out is how the other side’s evidence can be just as ironclad as its own.

The story takes place in a midsize town, where the grisly murder of a young boy appalls people and sets off a frenzy of speculation. The latter hits tsunami proportions when police arrest Terry Maitland, a high school teacher and longtime Little League coach who is — or, at least, had been — considered a cornerstone of the community.

The police are sure they have the killer. His DNA was found in the blood at the scene, and witnesses reported seeing him running from the area. The police are so cocky about their case that they round up Terry without even bothering to find out if he has an alibi. It’s a decision they quickly regret.

His alibi turns out to be a doozy. He was at an out-of-town conference with three of his fellow teachers. Not only do his co-workers vouch for him, but there’s even a tape showing Terry at a conference meeting held at the same time the murder took place.

The young, headline-seeking district attorney doesn’t care about the contradictions. He sees everything strictly in terms of the attention it can bring to his career. But the veteran detective on the case, Ralph Anderson, is troubled on personal and professional fronts. He’s known Terry for years; in fact, as a youngster, his college-age son — who’s infuriated with his father for the arrest — played baseball for Terry and refuses to believe that he could be involved in the murder. And although Ralph would never admit it to his son, his own doubts about it grow daily.

Then, in the turn of a page, none of it matters anymore — at least, not to mystery buffs. It becomes clear that there is not going to be any earthly solution to the case. All the effort we’ve put into analyzing the situation is now worthless. And all of Ralph’s figurative detective legwork becomes literal as he starts chasing a boogeyman.

Because we no longer have to focus on solving the case, we’re free to ponder the next question: Who is the book aimed at? Mystery fans likely are going to be frustrated by the fact that King changes all the rules mid-novel and provides himself with an easy out to what had been an intriguing dilemma. And it’s hard to see many occult fans being willing to wade through a couple hundred pages of a police procedural before getting to the spooky stuff.

The book is well written; it is, after all, from King. But in an unintended irony, that only adds to our disappointment when the narrative and tone radically change. We have become invested in the characters and their stories, and we feel abandoned when they run off in a different direction.

The theory of reaching out to divergent audiences might make sense from a marketing vantage; you can sell twice as many books that way. But the flip side is that you also can disappoint twice as many readers.

Perhaps King might have been better off making “The Outsider” two separate stories with shared characters. That way, we could focus on just the part that interests us and ignore the rest. Many readers probably will do that anyway.

The Outsider
By: Stephen King.
Publisher: Scribner, 561 pages, $30