A federal judge has dealt a significant blow to the New York police department's controversial stop-and-frisk policy, declaring that officers had "systematically" engaged in unconstitutional activity by searching residents outside thousands of private apartments in the city.

Manhattan federal court judge Shira Scheindlin said the NYPD must immediately halt the practice of conducting trespass stops at certain buildings in the Bronx neighbourhood. Campaigners said the policy unfairly targeted black and Latino householders who were placed "under siege" in their homes.

"While it may be difficult to say when precisely to draw the line between constitutional and unconstitutional police encounters such a line exists, and the NYPD has systematically crossed it when making trespass stops outside buildings," Scheindlin wrote in a 157-page ruling.

Scheindlin said that a lack of training within the NYPD may have contributed to the practice of unlawful stops outside buildings that formed part of the clean halls program, under which landlords can give police officers the right to patrol in and around private residential buildings.

"The evidence of numerous unlawful stops at the hearing strengthens the conclusion that the NYPD's inaccurate training has taught officers the following lesson: stop and question first, develop suspicions later," she wrote in her decision.

Operation Clean Halls, introduced in the Bronx in the 1990s, has the aim of combating illegal activity in thousands of apartment buildings in high-crime areas. But the New York Civil Liberties Union reported that in a subset of Clean Halls buildings, police officers conduct regular floor-by-floor sweeps, called vertical patrols, and engage in particularly aggressive stop-question-frisk-and-arrest practices.

The ruling comes after a long-running campaign by New York activist groups including the NYCLU. Opponents argued that the NYPD's clean halls program led to officers stopping and arresting residents outside their own buildings.

"Today's decision is a major step toward dismantling the NYPD's stop-and-frisk regime," said NYCLU executive director Donna Lieberman.

"Operation Clean Halls has placed New Yorkers, mostly black and Latino, under siege in their own homes in thousands of apartment buildings. This aggressive assault on people's constitutional rights must be stopped."

Scheindlin's ruling came in the case of Ligon v City of New York, filed by the New York Civil Liberties Union, the Bronx Defenders, LatinoJustice PRLDEF and the Shearman & Sterling law firm.

Jaenean Ligon had complained that she and her sons were stopped by police on multiple occasions outside their apartment building in the Bronx without cause. The ruling is the one of a number of stop-and-frisk cases that have been brought against the NYPD.