# Weekly Challenge #4 Movies with limited actors

**URL:** <https://community.neo4j.com/t/weekly-challenge-4-movies-with-limited-actors/59120>\
**Category:** Archive\
**Tags:** migrated\
**Created:** [August 30, 2022, 4:14pm UTC](https://community.neo4j.com/t/weekly-challenge-4-movies-with-limited-actors/59120 "2022-08-30T16:14:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![TrevorS](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/trevors/32/23179_2.png) [@TrevorS](https://community.neo4j.com/u/TrevorS)\
**Post date:** [August 30, 2022, 4:14pm UTC](https://community.neo4j.com/t/weekly-challenge-4-movies-with-limited-actors/59120/1 "2022-08-30T16:14:12Z")

</div>

Welcome back to our Weekly challenge series!  
In keeping with the theme so far, this week's challenge is to tune the provided query.

Here's what you need to do!

- Create a recommendations sandbox at [sandbox.neo4j.com](http://sandbox.neo4j.com).
- Here is a query that returns a row for every movie that has 2 or fewer actors. **How can you make this query perform better?**

```auto
"PROFILE
MATCH (m:Movie)<-[:ACTED_IN]-(a:Person)
WITH m, collect(a.name) as actorNames
WHERE size(actorNames) <= $maxActors
RETURN m.title, m.year, actorNames"

```

**Solutions submitted between 8/30 and 9/9 will be eligible for a Neo4j T-Shirt drawing!**

Show us your ways to improve the query!

---

<div class="post-metadata">

**Author:** ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)\
**Post date:** [August 30, 2022, 4:25pm UTC](https://community.neo4j.com/t/weekly-challenge-4-movies-with-limited-actors/59120/2 "2022-08-30T16:25:01Z")

</div>

Hello [@TrevorS](https://community.neo4j.com/t5/user/viewprofilepage/user-id/4) 😊

```auto
:param maxActors => 2;

MATCH (m:Movie) 
WHERE 0 < size((m)<-[:ACTED_IN]-()) <= $maxActors 
RETURN m.title, m.year, [(m)<-[:ACTED_IN]-(a) | a.name] AS actorNames;

```

 ![Cobra_0-1661876690501.png](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/d/0/d02d23cb3f2621317ce829136e74367e33c19f26.png "Cobra\_0-1661876690501.png")

Regards,  
Cobra

---

<div class="post-metadata">

**Author:** ![tard\_gabriel](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/tard_gabriel/32/28033_2.png) [@tard\_gabriel](https://community.neo4j.com/u/tard_gabriel)\
**Post date:** [August 31, 2022, 7:23am UTC](https://community.neo4j.com/t/weekly-challenge-4-movies-with-limited-actors/59120/3 "2022-08-31T07:23:31Z")

</div>

```auto
size((m)<-[:ACTED_IN]-())

```

I don't think it's possible to beat that as suggested by [@Cobra](https://community.neo4j.com/t5/user/viewprofilepage/user-id/3843)

---

<div class="post-metadata">

**Author:** ![JoshKos](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@JoshKos](https://community.neo4j.com/u/JoshKos)\
**Post date:** [November 30, 2022, 5:25am UTC](https://community.neo4j.com/t/weekly-challenge-4-movies-with-limited-actors/59120/4 "2022-11-30T05:25:57Z")

</div>

Best I could do was 20,050 db hits haha, I believe adding the "0 \< " is unnecessary right? For size to return \< 0 wouldn't be possible? And it adds processing time (and somehow db hits?) so below is very very very barely faster 😉 hahah

MATCH (m:Movie)

WHERE size(()-[:ACTED\_IN]-\>(m)) \<= $maxActors

RETURN m.title, m.year, [(a)-[:ACTED\_IN]-\>(m) | a.name]
