Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #2794: Create Object from Two Arrays

In this guide, we solve Leetcode #2794 Create Object from Two Arrays in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

Given two arrays keysArr and valuesArr, return a new object obj. Each key-value pair in obj should come from keysArr[i] and valuesArr[i].

Quick Facts

  • Difficulty: Easy
  • Premium: Yes
  • Tags: JavaScript

Intuition

Each key aligns with the value at the same index, so we can pair them directly.

A single pass over both arrays builds the desired object.

Approach

Zip the two arrays together and insert each pair into a dictionary. Later duplicates overwrite earlier ones, matching object assignment behavior.

Steps:

  • Iterate through zip(keysArr, valuesArr).
  • Assign obj[key] = value for each pair.
  • Return the dictionary.

Example

Input: keysArr = ["a", "b", "c"], valuesArr = [1, 2, 3] Output: {"a": 1, "b": 2, "c": 3} Explanation: The keys "a", "b", and "c" are paired with the values 1, 2, and 3 respectively.

Python Solution

class Solution: def createObject(self, keysArr: List[str], valuesArr: List[int]) -> dict: obj = {} for k, v in zip(keysArr, valuesArr): obj[k] = v return obj

Complexity

The time complexity is O(n)O(n)O(n), and the space complexity is O(n)O(n)O(n).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy