InterviewLegend

Arrays, Strings & Hashing

Alex Chen26 min readUpdated 53 real interview reports
In this lesson
  • Clear the Track A assessment, which scores running time and edges, not just passing samples
  • Learn the four moves that cover this band, from complement hashing to a map plus a list
  • Spot the one trap each problem hides before it costs you the round
  • Carry each solution into its follow-up, up to a thread-safe cache

A Tesla coding screen for a software, full-stack, backend, or infrastructure role opens on familiar ground. You get an array to transform, a string to scan, a small structure to design. Candidate reports and vendor guides agree the bar sits below FAANG: the bank is medium-dominant over a light warm-up floor, and the problems are canonical rather than exotic. What that framing leaves out is the second half of the sentence. The code still has to be efficient, clean, and complete at the edges. The assessment auto-scores running time, so a quadratic answer that returns the right value still times out on the large input and loses the points.

This is the Track A spine. Tesla's coding loop is split by the org you applied to, and your level matters less. Software and backend get this standard data-structures bank. Firmware gets an embedded-C test with no LeetCode at all, and Autopilot gets a harder C++ and ML tier. Those two live in their own lessons. This is the volume track. An online assessment on Codility or HackerRank usually opens Track A, roughly three questions in 85 to 100 minutes, auto-scored on correctness and running time. It leads into a live coding screen of about an hour with a senior engineer. Reports vary on the exact round order. The grading emphasis does not.

The Tesla angle
On Track A, correctness is only the floor; the version you write is the grade: optimal complexity, clean structure, every edge handled. The problems in this band are short and well known, so correctness alone separates almost nobody. The score depends on which version reaches the screen. Optimal complexity beats a brute force that happens to pass. The empty input and the single element should not crash, and the names and structure should read like something you would ship. Get a correct version down, say which corners you are checking (the empty array, the duplicate value, the zero in the middle), then reach for the tighter approach. On this loop, walking the interviewer through the complexity is part of the answer they score.
  • Center of mass: Arrays, strings, hashing. Across candidate reports the dominant Track A cluster is arrays with two-pointer and sliding-window work, followed by strings and hashing. Medium-centered over a light warm-up floor. Almost every screen opens here.
  • The online assessment: ~3 questions, 85 to 100 min. Codility or HackerRank, auto-scored on correctness and efficiency together. A solution that times out on the large case loses points even when it returns the right answer on the small one. Take-homes were retired for SWE roles.
  • What is graded: Efficient, clean, edge-complete. Candidate reports call the bar approachable, yet the assessment still scores running time and clean structure alongside correctness. Name your edge cases before the interviewer prompts you.
  • The track split: Software vs firmware vs AI. This lesson is the software and backend Track A spine. Firmware runs an embedded-C test and Autopilot a harder C++ and ML tier, each in its own lesson. Prep for the track you applied to.

Four moves that cover the band

Nearly everything in this band is one of four moves. Each has a short algorithm and a single trap that separates correct code from almost-correct. Learn the move, then learn the trap, because on a loop that scores running time the trap is what Tesla grades.

Hash the complement

The first move trades a scan for a lookup. Two Sum is the type case: find the two positions whose values add to a target. The brute force checks every pair in a quadratic-time double loop, which passes the small case and quietly loses points on the large one. Walk the array once instead, and for each value ask a hash map whether its complement has already appeared. One pass, constant-time lookups, and the exercise exists to close that efficiency gap.

Slide a window over a hashed frontier

The second move drags a window across a string and keeps a hash of what is inside it. Longest Substring Without Repeating Characters is the recurring one: the longest stretch with no repeated character. The naive version reexamines every substring; the window version remembers where each character last appeared and jumps the left edge past the repeat, so each character is visited once. Spotting that the left edge only ever moves forward is what turns a quadratic scan into a linear one.

Build from running state

The third move builds the answer from running state rather than a formula with a hole in it. Product of Array Except Self asks for the product of every element but the current one, and forbids division, because the division one-liner breaks the moment a zero appears. Two passes fix it: one carrying the product of everything to the left, one folding in everything to the right. No division, no special case for the zero, nothing to explain away. When a one-line trick has an edge case, a second pass usually removes it.

A hash map plus a second structure

The last move is a small design ask, and Tesla's recurring one is the LRU cache. It is a bounded cache that evicts the least recently used entry when it runs out of room, with get and put both in constant time. A hash map alone gives the lookup but has no sense of order, so it cannot find the entry to evict cheaply. Pair it with a doubly linked list that keeps entries ordered by how recently they were touched, and both operations become a fixed handful of pointer moves. Hash map for lookup, partner structure for the thing the map cannot do: that is the pattern.

Every move here is short to write and easy to get almost right, and that gap between almost right and right is the score. Finding the trick is the easy part. Tesla checks that your code hits the optimal complexity, survives the corner you were meant to see, and reads like production. Type the correct version, test it against the edges yourself, then make it efficient. On a loop that grades running time, the last step is not optional.

Asked at Tesla

Four problems, one per move, the easy warm-up first. A test case gives the function's arguments as a JSON array and the expected return, except the LRU cache, whose cases are a sequence of method calls and what each returns. Write the base version and run it against the corners, then let the walkthrough take the follow-up the rest of the way.

1

Bounded Edge Cache: LRU Cache

MediumFrequently asked
Problem
A cache on a memory-constrained device holds at most capacity key-value pairs and evicts the least recently used entry when it runs out of room. Implement LRUCache(capacity), then:

get(key) returns the value for key, or -1 if it is absent. A successful get counts as a use, making key most recently used.

put(key, value) inserts or updates the value for key, and also counts as a use. If the cache is over capacity after the write, evict the least recently used entry.

Both get and put must run in O(1) average time.

Input: LRUCache(2); put(1,1); put(2,2); get(1); put(3,3); get(2); put(4,4); get(1); get(3); get(4)

Output: [null, null, null, 1, null, -1, null, -1, 3, 4]

put(3,3) evicts key 2, because get(1) had just made key 1 the more recent. So get(2) returns -1. put(4,4) then evicts key 1, now the oldest, so get(1) returns -1 while 3 and 4 remain.

How this uses the pattern

Hash map plus one more structure. The map gives O(1) lookup from key to node. A doubly linked list orders the nodes from most to least recently used. The entry to evict is always the tail node, and any used node can be unlinked and moved to the front in O(1).

Bounded Edge Cache: LRU Cache

Loading...
Click "Run" to execute your code

Test Cases

Input: [["LRUCache",[2]],["put",[1,1]],["put",[2,2]],["get",[1]],["put",[3,3]],["get",[2]],["put",[4,4]],["get",[1]],["get",[3]],["get",[4]]]Expected: [null,null,null,1,null,-1,null,-1,3,4]
Input: [["LRUCache",[2]],["put",[1,1]],["put",[2,2]],["put",[1,10]],["get",[1]],["put",[3,3]],["get",[2]],["get",[1]]]Expected: [null,null,null,null,10,null,-1,10]
Input: [["LRUCache",[1]],["put",[1,1]],["get",[1]],["put",[2,2]],["get",[1]],["get",[2]]]Expected: [null,null,1,null,-1,2]
Input: [["LRUCache",[2]],["get",[2]],["put",[2,6]],["get",[1]],["put",[1,5]],["put",[1,2]],["get",[1]],["get",[2]]]Expected: [null,-1,null,-1,null,null,2,6]
The Tesla angle
The LRU cache is Tesla's recurring data-structure design ask on Track A. It fits the company: a bounded cache in front of a store, on a device with a fixed memory budget, is the kind of edge-computer problem Tesla builds around. It is graded on a real O(1) get and put, so the interviewer wants the doubly linked list built by hand. That list is what makes eviction and reordering constant time without walking a chain. You can note that Python's OrderedDict gives the same behavior with no hand-built list, but be ready to build one anyway. The grade rests on the edge cases. Handle capacity 1. When you write a key that already exists, update and promote it; do not insert a duplicate or evict the wrong entry. And promote an entry on every get as well as every put, so the eviction order stays correct.
Take it further
Many clients hit the cache at once. Make it thread-safe, then say what to do when a single lock becomes the bottleneck.
2

The Warm-Up Hash: Two Sum

EasyCommonly asked
Problem
Given an integer array nums and an integer target, return the indices of the two numbers that add up to target. Exactly one pair sums to the target, and you may not use the same element twice. Return the indices in ascending order.

Input encoding. A test case is a JSON array [nums, target]. The function returns a list of two integers.

Input: nums = [2,7,11,15], target = 9

Output: [0,1]

2 + 7 = 9, at indices 0 and 1.

Input: nums = [3,2,4], target = 6

Output: [1,2]

2 + 4 = 6. Note that 3 + 3 is not allowed, since the same element cannot be reused.

Input: nums = [3,3], target = 6

Output: [0,1]

The two threes sit at different indices, so the pair is valid.

How this uses the pattern

Trade the second loop for a hash map. Walk the array once, and for each value check whether target minus that value has already been seen. If it has, you have the pair; if not, record the value and its index and keep going.

The Warm-Up Hash: Two Sum

Loading...
Click "Run" to execute your code

Test Cases

Input: [[2,7,11,15], 9]Expected: [0,1]
Input: [[3,2,4], 6]Expected: [1,2]
Input: [[3,3], 6]Expected: [0,1]
Input: [[-1,-2,-3,-4,-5], -8]Expected: [2,4]
Input: [[0,4,3,0], 0]Expected: [0,3]
Input: [[5,75,25], 100]Expected: [1,2]
Input: [[1,2,3,4], 7]Expected: [2,3]
The Tesla angle
Two Sum is the canonical warm-up, and Tesla reaches for it exactly because it is not tricky. It exists to watch you write clean hashing under a stopwatch. The interviewer wants to see you skip the quadratic double loop and go straight to the one-pass hash map. The online assessment scores running time, and the double loop is the classic way to time out on the large case. Say why the map wins (a lookup replaces a scan), handle the reuse rule so an element never pairs with itself, and you have shown the habit this band is graded on. The same shape hides behind any pair-finding ask. The prompt might talk about two sensor readings that sum to a threshold, or two line items that hit a budget. Look past the framing to the pair you are matching.
Take it further
The array now arrives sorted in ascending order, and the interviewer wants it solved without the extra hash map. Return the 1-indexed pair using O(1) extra space.
3

Longest Distinct Window: Longest Substring Without Repeating Characters

MediumCommonly asked
Problem
Given a string s, return the length of the longest substring that contains no repeated character. A substring is a contiguous run of characters.

Input encoding. A test case is a JSON array [s], a single string. The function returns an integer.

Input: s = "abcabcbb"

Output: 3

The longest run with no repeat is "abc", length 3. After that every window hits a repeat.

Input: s = "bbbbb"

Output: 1

Every character is a repeat, so the longest distinct run is a single "b".

Input: s = "pwwkew"

Output: 3

"wke" has length 3. Note that "pwke" is not a substring, because it is not contiguous.

How this uses the pattern

Slide a window and remember where each character last appeared. When the incoming character is already inside the window, jump the left edge to just past its previous position. Track the widest the window ever gets.

Longest Distinct Window: Longest Substring Without Repeating Characters

Loading...
Click "Run" to execute your code

Test Cases

Input: ["abcabcbb"]Expected: 3
Input: ["bbbbb"]Expected: 1
Input: ["pwwkew"]Expected: 3
Input: [""]Expected: 0
Input: ["abba"]Expected: 2
Input: ["dvdf"]Expected: 3
Input: ["tmmzuxt"]Expected: 5
Input: [" "]Expected: 1
The Tesla angle
This is the dominant Track A cluster, arrays and strings under a sliding window, and it is the cleanest place to show the efficiency the online assessment is scoring. The naive answer reexamines substrings and lands in quadratic time or worse; the linear window is what a passing score expects. It unlocks once you see the left edge never needs to move backward. Record where each character last appeared, and a repeat lets you jump the left edge forward in one step. Narrate that the window is a frontier that only ever advances, and you have shown the linear-time instinct. Tesla likes to pose this as a stream question, the longest span of distinct events or the longest burst of telemetry before a value repeats, but every version reduces to the same longest-clean-run window.
Take it further
The interviewer relaxes the rule mid-round, a common Tesla move: instead of no repeats, allow at most K distinct characters, each of which may repeat. Return the length of the longest substring that stays within K distinct characters.
4

Every Slot but Its Own: Product of Array Except Self

MediumCommonly asked
Problem
Given an integer array nums, return an array answer where answer[i] is the product of every element of nums except nums[i]. Solve it in O(n) time without using the division operator.

The array holds at least two elements, and each product fits in a signed 32-bit integer.

Input encoding. A test case is a JSON array [nums], a single list of integers. The function returns a list of integers.

Input: nums = [1,2,3,4]

Output: [24,12,8,6]

Each slot is the product of the other three: 2·3·4, then 1·3·4, then 1·2·4, then 1·2·3.

Input: nums = [-1,1,0,-3,3]

Output: [0,0,9,0,0]

Only the slot at the single zero is nonzero; it holds the product of the rest, (-1)·1·(-3)·3 = 9. Every other slot multiplies through the zero.

Input: nums = [2,3]

Output: [3,2]

Two elements, so each slot is simply the other one.

How this uses the pattern

Two passes, no division. Sweep left to right writing into each slot the running product of everything before it, then sweep right to left multiplying in the running product of everything after it.

Every Slot but Its Own: Product of Array Except Self

Loading...
Click "Run" to execute your code

Test Cases

Input: [[1,2,3,4]]Expected: [24,12,8,6]
Input: [[-1,1,0,-3,3]]Expected: [0,0,9,0,0]
Input: [[2,3]]Expected: [3,2]
Input: [[0,0]]Expected: [0,0]
Input: [[5,0,2]]Expected: [0,10,0]
Input: [[1,1,1,1]]Expected: [1,1,1,1]
Input: [[-2,3,-4]]Expected: [-12,8,-6]
The Tesla angle
Tesla forbids division here on purpose, and that is the exercise. Division tempts a one-line answer that quietly breaks on a single zero and breaks differently on two. With the shortcut banned, the test is whether you write clean, edge-complete code. The two-pass answer runs in O(n) with only the output array as extra space, which is the efficiency the online assessment rewards. The corner interviewers watch is the zero: with the two passes it needs no special case at all, because a zero is just another factor in the sweep. Name that, and you have shown the edge-completeness the loop grades. Whatever product story the prompt arrives in, underneath, it is an except-self aggregate, a running sum or running max over everything but the current index.
Take it further
The interviewer lifts the no-division rule and asks whether division makes the code cleaner. Show the O(1) extra-space division version, and enumerate the zero cases it now has to handle by hand.

Key Takeaways

  • ✓This is the Track A warm-up and phone-screen band, where efficiency and edge-handling are graded alongside correctness. A brute force that returns the right answer but exceeds the time limit on the large case still loses the points.
  • ✓Four moves cover it: hash the complement, slide a window over a hashed frontier, build from running state without the banned shortcut, and pair a hash map with a second structure. The algorithm is short in each; the trap is what earns the score.
  • ✓Name your edge cases before the interviewer prompts: the reused element in Two Sum, the left edge that must not move backward, the zero that needs no special case, the capacity-1 cache. Saying them out loud is the habit the loop rewards.
  • ✓Expect an escalation even on a warm-up: a sorted-input rewrite, a relaxed constraint, division reintroduced, or thread safety. Clear it cleanly and the loop moves on to trees and graphs.
Had your Tesla interview?Tell us what they asked. Reviewed before it goes in, never with your name.
Tesla interview guide

Know what Tesla asks before you walk in.

22 coding · 5 system design · 17 behavioral questions

Get Premium$49 a year · Cancel anytime
Or start freeNo card.

Get their questions first.

InterviewLegend is independent and not affiliated with or endorsed by any company it covers.