Besides his most recent trip to Quetta, Mr. Rahami visited Karachi, Pakistan, in 2005. Both of those cities’ reputations have become entwined with the militant groups who have sheltered there: Karachi as a haven for the Pakistani Taliban and Al Qaeda, and Quetta as the headquarters of the exiled Afghan Taliban leadership. But both cities are also home to generations of Afghans who have fled violence in their home country.

Much about his New Jersey life did seem unremarkable. Amarjit Singh, a limousine driver, was friends with Mr. Rahami at Edison High School. The person he knew, he said, was a determined student with an abundance of friends and a string of girlfriends. “Everyone seemed to like him,” he said. “Smart, funny, humble.”

He viewed the teenage Mr. Rahami as the prototypical immigrant, teetering between two worlds. While he wore jeans and sweatshirts like his friends and worked at a Pathmark supermarket after school, he preferred Afghan music and prayed at the mosque on Friday. Collisions between those worlds sometimes led to rifts with his father, who was more religious and traditional. “The two of them would argue,” Mr. Singh said. “There seemed to be a lot of tension.”

His father was especially displeased when Mr. Rahami had a daughter with a high school girlfriend, according to friends. Reached at her home on Monday night, she declined to comment. “My heart is just broken,” said the woman, who The New York Times is not identifying. “I don’t even know what to think.”

After high school, Mr. Singh said that he and Mr. Rahami had worked together for a while on the night shift at Royal Fried Chicken in Newark. Mr. Singh worked the fryer in the back. Mr. Rahami handled the register. Whenever Mr. Singh got into a dispute with customers, he remembered Mr. Rahami stepping in as the peacemaker. In recent years, the two drifted apart. Mr. Singh was also aware that Mr. Rahami had traveled abroad and that he had become more religious and had taken to wearing Muslim robes.

The events on Monday were not Mr. Rahami’s first encounter with law enforcement. He was arrested in 2014 on weapons and aggravated assault charges for allegedly stabbing a relative in the leg in a domestic incident, according to court documents. He spent over three months in jail on the charges, according to a high-ranking law enforcement official with knowledge of the investigation. A grand jury, however, declined to indict Mr. Rahami. He also spent a day in jail in February 2012 for allegedly violating a restraining order, the official said.