Another interpretation: "There was a little city" (Ecclesiastes 9:14) — this is Egypt. "And few people within it" — these are the brothers of Joseph. "And there came against it a great king" — this is Joseph. "And he built against it great siegeworks" — he said to them, "You are spies" (Genesis 42:9). "Now there was found in it a poor wise man" (Ecclesiastes 9:15) — this is Judah. "And he, by his wisdom, delivered the city" — "Then Judah drew near to him" (Genesis 44:18).
Judah the Poor Wise Man Who Delivered the City of Egypt
Midrash Tanchuma Buber, Vayigash 1:4
ד"א עיר קטנה, זה מצרים. ואנשים בה מעט אלו אחי יוסף, ובא אליה מלך גדל זה יוסף, ובנה עליה מצודים גדולים, א"ל מרגלים אתם, ומצא בה איש מסכן וחכם זה יהודה, ומלט הוא את העיר בחכמתו, ויגש אליו יהודה.