WASHINGTON — A federal judge on Monday partly blocked the National Security Agency’s program that systematically collects Americans’ domestic phone records in bulk just weeks before the agency was scheduled to shut it down and replace it. The judge said the program was most likely unconstitutional.

In a separate case challenging the program, a federal appeals court in New York on Oct. 30 had declined to weigh in on the constitutional issues, saying it would be imprudent to interfere with an orderly transition to a replacement system after Nov. 29.

But on Monday, in a 43-page ruling, Judge Richard J. Leon of United States District Court for the District of Columbia wrote that the constitutional issues were too important to leave unanswered in the history of the program, which traces back to after the Sept. 11 terrorist attacks and came to light in 2013 in leaks by Edward J. Snowden, the former intelligence contractor.

“With the government’s authority to operate the bulk telephony metadata program quickly coming to an end, this case is perhaps the last chapter in the judiciary’s evaluation of this particular program’s compatibility with the Constitution,” he wrote. “It will not, however, be the last chapter in the ongoing struggle to balance privacy rights and national security interests under our Constitution in an age of evolving technological wizardry.”