Late last year, a little-known deputy in the San Francisco Public Defender’s Office asked for a meeting with one of the city’s top political consultants for progressive candidates and campaigns.

Fittingly, they talked in the back garden of Progressive Grounds, a coffee shop on Cortland Avenue in Bernal Heights. Chesa Boudin wanted Jim Stearns to help him with a long-shot endeavor: becoming San Francisco’s next district attorney. Boudin was hopeful, despite never having prosecuted a case and having a controversial background that included an imprisoned father and a stint working for Hugo Chávez, the late president of Venezuela who was criticized for trampling human rights.

“Candidates always want to know, ‘What’s the game plan for winning?’” Stearns said. “I said, ‘It’s a really long way from where you are now to the finish line.’”

Stearns didn’t even want the gig — he’d worked on Kamala Harris’ campaign for district attorney in 2003 and knew district attorney races are more complex than regular political races like mayor and supervisor.

“Literally after spending an hour with Chesa, I was extremely inspired by both his story and his agenda for change, his intelligence and charisma,” Stearns said. “I was like, ‘Let’s do this.’”

And, boy, did they. Over the weekend, it became official: In less than a year, Boudin had gone from that unknown hopeful to San Francisco’s district attorney-elect, beating Suzy Loftus, the candidate so formidable, former District Attorney George Gascón announced he wouldn’t seek re-election shortly after she entered the race.

So how did Boudin do it?

Asked this question, the winner himself texted, “Hard work, a broad, diverse coalition of grassroots supporters and a consistent, principled message rooted in a lifetime of experience with a broken system.”

That’s surely true. But elections also hang on political strategy — or lack thereof. Conversations with Stearns, as well as Loftus’ campaign consultant and other political insiders, pointed to several key reasons that the neck-and-neck race finally tipped Boudin’s way.

The story: Boudin and Mayor London Breed, who narrowly won last June’s super-tight race to finish the late Mayor Ed Lee’s term, don’t have much in common politically. But they do share a key similarity that helped them win: an inspiring story they embraced and talked about often on the campaign trail.

You know both stories by now unless you’ve been living under a rock.

Breed grew up in rough public housing projects, lost her sister to a drug overdose and saw her brother go to prison for manslaughter. She overcame all that to become mayor of her own hometown — only the second woman ever and the first woman of color.

Boudin’s story is even more movie-worthy. His parents were left-wing radicals in the Weather Underground and served as getaway drivers in an October 1981 robbery that wound up killing three people. Boudin, 14 months old at the time of the incident, was adopted by Bill Ayers and Bernardine Dohrn, also Weather Underground radicals. He graduated from Yale and Oxford University, where he was a Rhodes scholar.

His biological father remains in prison in New York; Boudin and his fiancee visited him there after the election.

Stearns said he and Boudin at the outset agreed on two strategies. One was that his story was “foundational to his life and his mission,” and he would need to talk about it early and often. Two was that he would speak openly and proudly about being a public defender. He could link both to his desire to fix the broken criminal justice system.

“It was just this gut instinct on his and my part that we didn’t want to sugarcoat anything,” Stearns said. “If people were looking for change, that was going to be real proof that he was completely different than all the other candidates.”

The appointment: After Gascón announced he wasn’t running for another term, the race was supposed to be the first wide-open one for district attorney in more than a century. But then Gascón quit Oct. 18 to seek the same job in Los Angeles, 18 days before election day.

Breed almost immediately appointed Loftus to the post of interim D.A., though she could have waited until Nov. 5 and appointed the winner. The gamble gave Loftus a couple of weeks to prove she could do the job, but it also put a big target on her back. I heard a lot of voters say they were turned off by the mayor putting her thumb on the scale, and it’s likely Loftus lost votes because of it.

“A lot of voters saw it as an attempt to rig the system,” said Jon Golinger, a progressive political consultant and a professor of election law at Golden Gate University. “Voters don’t like to feel like the decision was taken away from them.”

Ranked-choice voting: A lot of political observers, including, I’ll confess, yours truly, thought Boudin might win in first-choice votes, but assumed Loftus would be the ultimate winner because of ranked-choice voting.

The two other candidates, Leif Dautch and Nancy Tung, seemed far more aligned with Loftus. Voters who backed those two would surely give Loftus a nod and leave Boudin, the far-left public defender, off their ballots altogether.

Right? Nope.

Dautch was eliminated first, and among his supporters, roughly the same number of people voted for Loftus second as didn’t choose anyone for second place. Boudin and Tung got big shares of his second-choice votes.

Tung was eliminated next. Of her 46,600 votes, Loftus got just 17,358 and Boudin got 13,151. Others had either ranked Dautch or nobody. This was surprising considering Tung was more conservative than Loftus, with Boudin on the opposite end of the spectrum.

Once ranked-choice voting was complete, Boudin beat Loftus by 2,825 votes, showing how she could have easily won by grabbing a relatively small chunk of the other candidates’ backup choices. So why didn’t she make it official and pair up with Dautch or Tung?

Jason McDaniel, associate professor of political science, said it was simply poor political strategy on the part of the Loftus campaign.

“They needed to do a ranked-choice voting strategy,” McDaniel said. “Voters can listen to those signals, but the candidates have to send them in a strong, clear way.”

In a low-turnout, under-the-radar election, candidates couldn’t assume voters would know who was politically aligned.

Tung was a natural choice for Chinese voters, who make up a third of the city’s voters, and Boudin campaigned heavily in the Chinese community. He speaks some Cantonese and Mandarin and won the endorsement of the Sing Tao Daily, the largest Chinese language newspaper in the Bay Area. That could be a reason he won a lot of Tung’s second-choice votes, even though their politics were at opposite ends of the candidate spectrum.

Nicole Derse, Loftus’ campaign consultant, said Loftus did have conversations with the other candidates, but an official pairing became a no-go after Breed appointed her as interim district attorney.

“Once she got the appointment, it became really difficult to build those bridges with those candidates, because they were really focused on critiquing Suzy’s appointment and less focused on communicating the positions that they shared,” Derse said.

That seems late in the game to develop a ranked-choice voting strategy anyway. I moderated a KRON-TV debate among the four candidates on Sept. 27 and asked each one who would get their second-choice vote. Tung said Loftus. Loftus said no one would get her second-choice vote.

The excitement: Of all the candidates, Boudin arguably generated the most enthusiasm among his supporters — and gained national attention for being part of a movement of electing progressive prosecutors.

He won the endorsements of U.S. Sen. Bernie Sanders, singer John Legend and the co-founders of Black Lives Matter. A lot of money from outside San Francisco and California came in to support him, including from civil rights activist Shaun King’s Real Justice PAC.

(Those over-the-top Police Officers Association mailers against Boudin likely backfired — and they didn’t even say whom to vote for as an alternative.)

Nathan Ballard, a Democratic strategist who supported Loftus, said Boudin seems cut from the same cloth as the late Public Defender Jeff Adachi, who was smart, charismatic, hard-working and had an unshakable far-left ideology.

“Chesa is the new progressive superstar who’s filling the big shoes that Jeff Adachi left behind,” Ballard said. “That sort of authenticity is compelling, especially to progressive voters who tend to look for a savior.”

Derse said voters could be let down, though, once that sheen has worn off.

“The national press painted Chesa as a golden boy, but they didn’t focus on what he was actually going to do as D.A.,” she said. “Too few San Franciscans probably understand his priorities and his plans as district attorney. They probably don’t know that he’s never prosecuted a case. They probably don’t know the extent of the crimes that he doesn’t want to try.”

Winning was unlikely, but Boudin has done it. Now he has an even harder job: helping to run San Francisco.

San Francisco Chronicle columnist Heather Knight appears Sundays and Tuesdays. Email: hknight@sfchronicle.com Twitter: @hknightsf Instagram: @heatherknightsf