As New York City's three-term Mayor, multi-billionaire Michael Bloomberg, reaches the twilight of his 12-year reign over the Big Apple, the fight to fill his shoes entered a nail-biting phase this week.

On Wednesday, the candidates contested their party primaries. Joe Lhota, former transport chief and deputy to "tough-on-crime" mayor Rudy Giuliani, easily won the Republican nomination.

The big question, in a city where registered Democrats outnumber Republicans six to one, was who Democrats would select.

By Wednesday, city ombudsman Bill de Blasio appeared to have the Democratic nomination. "We are better as a city when we make sure that everyone has a shot," he told jubilant supporters. "We begin tonight."

With all precinct votes counted, De Blasio had 40.13 per cent. If his wafer-thin win slips below 40 per cent when absentee votes are tallied by next week, he will face a challenge from second-placed Bill Thompson.

After a generation of business-friendly Republican mayors - first Rudy Giuliani and then Bloomberg, a Democrat turned Republican turned independent - Democrats can taste victory at the November 5 election.

De Blasio, the unknown candidate who has taken the lead from fourth place within a month, offered a clean break from the Bloomberg era.

His populist "Tale of Two Cities" strategy hammered home arguments that the Mayor presided over a city of gaping inequalities.

"Nearly 400,000 millionaires call New York home, while nearly half of our neighbours live at or near the poverty line," said De Blasio. "Our middle class isn't just shrinking - it's in danger of vanishing altogether."

His claim carries a punch in a city where the income of the richest 1 per cent rose from 12 per cent of the city total in 1980 to 39 per cent in 2012, and the poverty rate in 2011 was 22.5 per cent, according to the Fiscal Policy Institute.

While Bloomberg has added jobs and attracted tourists, middle- and low-class incomes have flatlined. The FPI says long-term unemployment among blacks, Latinos and older workers exceeds the United States average and a quarter of the labour force - excluding Wall Street - receive wages insufficient to keep a family of four above poverty levels.

Homelessness is at record levels with 50,000 sleeping in shelters, more than at any time since the Great Depression of the 1930s. Between 2000 and 2010, family incomes in the city's wealthiest neighbourhoods soared 55 per cent; they dropped 3 per cent in middle-class areas and 0.2 per cent in poorer areas.

Painting Bloomberg as a "vanity mayor" obsessed with smoking bans and bicycle lanes, De Blasio promised to raise income tax by about 0.5 per cent for New Yorkers earning over US$500,000 ($614,000), using this revenue to fund universal daycare and after-hours school programmes for the less fortunate. He also promised 200,000 new affordable housing units, in a city where poorer residents can spend years on waiting lists for city-owned apartments.

The Mayor fired back: "The way to help those who are less fortunate is, number one, to attract more very fortunate people. They are the ones that pay the bills." But the Mayor's off-hand comment that it would be great if "all the Russian billionaires moved here" made him look out of touch.

De Blasio also tapped into widespread concerns about the New York Police Department's controversial "stop and frisk" tactics, used against 600,000 blacks and Latinos in 2011 alone. The procedure was ruled unconstitutional by a federal judge last month. A TV ad, in which De Blasio and his black wife advise their teenage son what to do if stopped by police, went viral, catapulting him into the lead and drawing the ire of Bloomberg.

The Mayor's claim, made in New York magazine, that De Blasio was running a "class warfare and racist" campaign backfired, gilding De Blasio's image as the man the ancien regime feared.

Nonetheless, stop and frisk, which police argue takes guns off the street, may have contributed to a dramatic fall in crime rates, notably murder, rape, burglary and robbery, on Bloomberg's watch. In 2011 18,159 burglaries were reported out of a population of 8.2 million (Auckland, with a population of 1.48 million, recorded 9037 burglaries that year).

De Blasio's critics ask if the candidate can walk his talk and brand him an opportunistic flip-flopper. He will also need political chops to woo the state legislature in Albany. His liberal credentials were an asset in the primaries, drawing comparisons to Barack Obama's breakout 2008 campaign, but he must court the centre.

Conservatives, spooked by De Blasio's tax-the-rich talk, will likely coalesce behind Lhota, a socially liberal New York policeman's son. Credited with getting the city's subway open after Hurricane Sandy, Lhota has stoked fears of De Blasio as an inexperienced ideologue whose election would unleash a dystopian replay of the last Democratic mayoral era of high crime and urban decay.

Cast as Giuliani's successor, Lhota has backing from conservatives such as oil tycoon David Koch. But city demographics have shifted since the 1990s as whites leave the city and minorities grow, favouring De Blasio.

Whoever wins office in November will face daunting challenges. Chief among them is the city budget - US$70 billion for 2012-13 and leavened with surpluses to fund city healthcare. Besides housing, education and crime, the victor must navigate a fiscal labyrinth with a US$2 billion deficit, plus pay demands from the 300,000 city employees including police, firefighters and teachers, now working without contracts.

Then there are wider issues. One is immigration reform, crucial to a city where migrants are a historic motor of growth. Another is climate change.

While Lhota is quiet, De Blasio stressed sustainability. He also addressed the possibility of future storms like Sandy, which killed 44 New Yorkers and caused US$19 billion in damage. In a metropolis where 800,000 are threatened by rising sea levels by 2050 - the World Bank rates New York third in 136 flood-vulnerable cities - deciding how to protect the city's status as the world's financial hub is an awesome task.

In 2009, the American Society of Civil Engineers said US$10 billion would reduce vulnerability. New Yorkers have never been shy when it comes to boasting about their city's potential. But defending them against climate change is a huge ask that will require vision, political will, deep pockets and plenty of chutzpah.