When Jed Mercurio, writer and creator of BBC2's Line of Duty, sat down to watch footage shot for the final episode of the crime series, he realised something was missing. A vital bit of information needed to make the resolution gel was not there.

It was a tense moment, almost equal to those Line of Duty's three million-plus viewers now expect from its suspenseful plotting. Mercurio picked up the phone nervously to find out whether the actor involved was still in Belfast, where the series is made. "Luckily, they had not flown out yet. So we grabbed them and quickly filmed another sequence, which was then edited seamlessly into a scene," he said.

The writer realises that the finale of the hit second series will be widely scrutinised for inconsistencies. It is a pressure, but one Mercurio is enjoying: "It is fantastic that so many people are talking about it, trying to work out whether Keeley Hawes's DI Denton, is guilty."

When the actors signed up, there was still doubt about the ending. "I hadn't worked it all out yet. I wasn't 100% clear about the denouement. But we did tell the actors before we started filming, so they knew how to play it."

Such is the level of public anticipation, episode to episode, that World Productions, which makes the show for the BBC, has had complaints about its trailers. "We decided not to run those 'next week' tasters after each episode because they give too much away, but we do have to let people know when the show is on," said Simon Heath, Mercurio's producer and collaborator.

Even the writing process was a switchback ride. "Jed and I had an unusual way of working. We talked about each episode in turn before Jed wrote it. We wanted to make sure we were never further forward than the audience would be as they watched it," said Heath.

They are both fans, he said, of the bold opening sequence and the closing twist. "Having a cliffhanger at the end of an episode went out of fashion; people felt that it was too tarty. But we like them."

The success of series one meant a quick recommission, so Heath and Mercurio are particularly pleased that several TV reviewers say their second outing is even better than the first.

"Certain things we learned. We lost some of the social comment from the first series," said Heath. "This one is more a straight thriller. And we knew because of the reaction to the early killing of Gina McKee's character the first time that a shock that hasn't been signalled can really work."

So policewoman Georgia Trotman, played by Jessica Raine from Call the Midwife, was pushed out of a window early in series two. "It was cull the midwife," as we said."

Mercurio – a fan of Danish shows The Killing and The Bridge, who had his first success in the 1990s with hospital drama Cardiac Arrest – said writing Line of Duty was a bit like Newtonian mechanics: "If you have a lot of forward motion in a story, any sudden change of direction will really be felt."

At the centre of the drama is Superintendent Ted Hastings, who runs the internal investigations unit and is played by Adrian Dunbar. "Viewers go through the story with Hastings," said Dunbar, ahead of the final episode. "I am helped by the dilemmas presented in the writing. It gives you the character, the way he faces moral problems that come up."

Dunbar is loving the role – and the fact that strangers have started calling out to him in the street, "Don't tell me!"

In the show each character is placed in some kind of jeopardy – they all have something to lose. But Hawes's angry DI Denton has developed into a new order of malevolent screen presence.

"We wanted to create big characters and knew this was a brilliant role by the first draft," said Heath. "Then the idea of getting someone so unexpected for the part made it even better. But even when we watched the tapes we didn't realise what a pull Keeley would have; how she would capture imaginations."

A third series has not yet been commissioned, but Mercurio wants the internal investigations unit to take centre stage again. In a sea of TV crime, it is the element of cop-on-cop detection that sets the show apart, he believes.

Heath agrees that there is no shortage of crime series, but says it is a broad genre: "If you don't like crime stories, you are going to struggle with British drama, it's true. But people do like them. Our show is about an institution and corruption. It is more thriller than cop show, I think."

Dunbar feels the police procedural format offers a platform for the most dramatic forms of human behaviour: "In this show there is a storyline to follow, and characters that you like one moment and go off the next. The viewer's sympathies are thrown around the place – and that's what real life can be like."